PHP - Ajax 获取数据而不刷新

标签 php css ajax

我正在使用这个完全相同的教程 http://www.w3schools.com/php/php_ajax_database.asp

创建了 2 个文件,一个 HTML 文件和一个 PHP(Conn/Query 等)它运行良好,但唯一的问题是表格似乎失去了 CSS 格式。

有什么想法吗?谢谢

PHP 文件

 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>";
}

至于HTML文件

<!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>

最佳答案

您在 PHP 文件中遗漏了一个 div。

使用以下代码:

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

代替

echo "</table>";

在那个div中负责CSS格式

关于PHP - Ajax 获取数据而不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16457917/

相关文章:

javascript - JavaScript、AJAX 代码显示数据有问题

html - 如何删除具有内联 block 样式的列表项之间的空格

php - 在 PHP 中使用 schemaValidate() 验证 XML 时如何将警告消息作为字符串获取?

php - Laravel 在单个子句中使用多个 where 和 sum

php - 如何根据用户 ID 获取一张表中时间戳的最新条目

jquery - 无法多次从 popup.js 元素检索 css 属性

IE 中的 HTML、CSS 问题

jquery - 如何在 ruby​​ 中告诉 ajax 请求

ajax - (drupal/whitehouse.gov) 如何动态加载节点内容而不重新加载页面?

PHP范围问题