【问题标题】:Live search in table PHP表 PHP 中的实时搜索
【发布时间】:2018-06-30 18:12:48
【问题描述】:

我创建了一个简单的搜索引擎,但我希望每当我搜索名称时它都会按原样显示在表格中。问题是其他数据只会隐藏,我不知道该怎么做。 这是我的 HTML 代码。

<form name="view" method="post" div class="frm1">
<table align="center" class="record_table" >
<tr bgcolor="#006600" height="50" style="color:#FFF;">
  <th>&nbsp;</th>
  <th>Name</th>
  <th>Email</th>
  <th>Packages</th>
  <th>Contactno</th>
  <th>Gender</th>
  <th>File</th>
  <th>Address</th>
  <th>Action</th>
</tr>
Search:
<input type="text" size="20" name="search" onKeyup="srch();">
<div id="d1"</>
<script>
function srch()
{
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","dbsearchfile.php?view="+document.view.search.value,false);
xmlhttp.send(null);
document.getElementById("d1").innerHTML=xmlhttp.responseText;
}
</script>

这是我的 PHP 代码。

<?php
$view=$_GET['view'];
mysql_connect("localhost","root","");
mysql_select_db("pm");
$res=mysql_query("select * from tblreservation where Name like ('$view%');");
echo "<table>";
while($row=mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>";
echo "<div class=frm1>";
echo "<a href='#'>". $row['Name'] . "</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>

【问题讨论】:

  • 您的 HTML 有语法错误 (&lt;div id="d1"&lt;/&gt;),您的 php 代码有语法错误,您容易受到 SQL 注入攻击,您的 ajax 代码不正确并且不等待来自的响应服务器等...
  • “按原样显示在表格中”是什么意思?您是说要在不重新加载页面的情况下根据某些搜索过滤原始表格数据吗?
  • 按名称实时搜索表行。
  • 你错过了在 mysql_select_db("pm); 中关闭 "

标签: php jquery html-table


【解决方案1】:

见:

<div id="d1"</>

正确:

<div id="d1"></div>

$res=mysql_query("select * from tblreservation where Name like ('$view%');");

建议:

$res=mysql_query(" select * from tblreservation where Name like '".$view."%'; ");

【讨论】:

  • 实时查表php和mysql
【解决方案2】:

我认为在 PHP 端做这件事太复杂了。我建议使用 jQuery.FilterTable 插件留在客户端并在那里进行过滤。您将获得无需重新加载页面的实时搜索。但是使用 Ajax 可以重新获取。

插件:http://sunnywalker.github.io/jQuery.FilterTable/

演示:http://sunnywalker.github.io/jQuery.FilterTable/filtertable-sample.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 2012-09-08
    • 2012-06-11
    • 1970-01-01
    相关资源
    最近更新 更多