PHP - Ajax获取数据而无需刷新

PHP  -  Ajax获取数据而无需刷新

问题描述:

I am using this exact same tutorial http://www.w3schools.com/php/php_ajax_database.asp

Created 2 files, one HTML file and one PHP ( Conn/Query ect..) it working great but the only issue is that the table seems to loose its CSS formatting.

Any ideas why? Thanks

PHP FILE

 echo "<div class='widget'>
<div class='whead'><h6>Search Result</h6><div class='clear'></div></div>
<div id='dyn' class='hiddenpars'>
    <a class='tOptions' title='Options'><img src='assests/images/icons/options' alt='Filter' /></a>
    <table cellpadding='0' cellspacing='0' border='0' class='dTable' id='dynamic'>
    <thead>
    <tr>
        <th>Name</th>
        <th>Family</th>
        <th>Age</th>
        <th>Gender</th>
        <th>Location</th>

    </tr>
    </thead>
    <tbody>";

if ( count($result) ) { 
foreach($result as $row) {
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Family'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['Location'] . "</td>";
echo "</tr>";
}

echo "</table>";
}

As for the HTML File

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-  scale=1.0, user-scalable=0" />
 <title>Test</title>

 <link href="assests/css/styles.css" rel="stylesheet" type="text/css" />


 <script type="text/javascript"   src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>

 <script type="text/javascript" src="assests/js/plugins/forms/ui.spinner.js"></script>
 <script type="text/javascript" src="assests/js/plugins/forms/jquery.mousewheel.js">  </script>

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
 <script type="text/javascript" src="assests/js/plugins/tables/jquery.dataTables.js"></script>
 <script type="text/javascript" src="assests/js/plugins/tables/jquery.sortable.js">  </script>
 <script type="text/javascript" src="assests/js/plugins/tables/jquery.resizable.js"></script>
 <script>
 function showName(str)
 {
   if (str=="")
      {
 document.getElementById("txtHint").innerHTML="";
 return;
      } 
 if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
      }
  else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 xmlhttp.onreadystatechange=function()
 {
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
 {
 document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
 }
 }
 xmlhttp.open("GET","getName.php?q="+str,true);
 xmlhttp.send();
 }</script>

 </head>
 <body>
 <div id="txtHint">

 </div>
 </body>
 </html>

You have missed a div in PHP file.

Use the following code:

echo "</tbody></table></div>";

Instead of

echo "</table>";

In that div is responsible for the CSS format

looks to be css class issue remove echo " from php file and try using

document.getElementById("txtHint").html =xmlhttp.responseText;