使用PHP AJAX和XML进行实时搜索

使用PHP AJAX和XML进行实时搜索,php,jquery,mysql,ajax,xml,Php,Jquery,Mysql,Ajax,Xml,我在web上进行了搜索,在W3C上找到了这个链接,它告诉您如何使用Php、Ajax和XML()进行实时搜索。我可以理解他们在代码上做了什么,下面是 search.php文件 <?php include_once 'header.php'; ?> <html> <head> <script> function showResult(str) { if (str.length==0) { document.getElementById("l

我在web上进行了搜索,在W3C上找到了这个链接,它告诉您如何使用Php、Ajax和XML()进行实时搜索。我可以理解他们在代码上做了什么,下面是

search.php文件

<?php
include_once 'header.php';
?>
<html>
<head>
<script>
function showResult(str) {
  if (str.length==0) {
    document.getElementById("livesearch").innerHTML="";
    document.getElementById("livesearch").style.border="0px";
    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("livesearch").innerHTML=xmlhttp.responseText;
      document.getElementById("livesearch").style.border="1px solid #A5ACB2";
    }
  }
  xmlhttp.open("GET","livesearch.php?q="+str,true);
  xmlhttp.send();
}
</script>
</head>
<body>

<form>
<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>

</body>
</html> 
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");

$x=$xmlDoc->getElementsByTagName('link');

//get the q parameter from URL
$q=$_GET["q"];

//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
  $hint="";
  for($i=0; $i<($x->length); $i++) {
    $y=$x->item($i)->getElementsByTagName('title');
    $z=$x->item($i)->getElementsByTagName('url');
    if ($y->item(0)->nodeType==1) {
      //find a link matching the search text
      if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
        if ($hint=="") {
          $hint="<a href='" .
          $z->item(0)->childNodes->item(0)->nodeValue .
          "' target='_blank'>" .
          $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        } else {
          $hint=$hint . "<br /><a href='" .
          $z->item(0)->childNodes->item(0)->nodeValue .
          "' target='_blank'>" .
          $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        }
      }
    }
  }
}

// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
  $response="no suggestion";
} else {
  $response=$hint;
}

//output the response
echo $response;
?> 

函数显示结果(str){
如果(str.length==0){
document.getElementById(“livesearch”).innerHTML=“”;
document.getElementById(“livesearch”).style.border=“0px”;
返回;
}
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}else{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“livesearch”).innerHTML=xmlhttp.responseText;
document.getElementById(“livesearch”).style.border=“1px solid#A5ACB2”;
}
}
open(“GET”,“livesearch.php?q=“+str,true”);
xmlhttp.send();
}
以及livesearch.php文件

<?php
include_once 'header.php';
?>
<html>
<head>
<script>
function showResult(str) {
  if (str.length==0) {
    document.getElementById("livesearch").innerHTML="";
    document.getElementById("livesearch").style.border="0px";
    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("livesearch").innerHTML=xmlhttp.responseText;
      document.getElementById("livesearch").style.border="1px solid #A5ACB2";
    }
  }
  xmlhttp.open("GET","livesearch.php?q="+str,true);
  xmlhttp.send();
}
</script>
</head>
<body>

<form>
<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>

</body>
</html> 
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");

$x=$xmlDoc->getElementsByTagName('link');

//get the q parameter from URL
$q=$_GET["q"];

//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
  $hint="";
  for($i=0; $i<($x->length); $i++) {
    $y=$x->item($i)->getElementsByTagName('title');
    $z=$x->item($i)->getElementsByTagName('url');
    if ($y->item(0)->nodeType==1) {
      //find a link matching the search text
      if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
        if ($hint=="") {
          $hint="<a href='" .
          $z->item(0)->childNodes->item(0)->nodeValue .
          "' target='_blank'>" .
          $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        } else {
          $hint=$hint . "<br /><a href='" .
          $z->item(0)->childNodes->item(0)->nodeValue .
          "' target='_blank'>" .
          $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        }
      }
    }
  }
}

// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
  $response="no suggestion";
} else {
  $response=$hint;
}

//output the response
echo $response;
?> 

但是他们接下来要做的是创建一个xml页面(),该页面包含他们想要搜索的所有数据,但在我的例子中,我想要搜索我的数据库表,我使用的是SQL。我试图做一些编码,但我找不到如何从查询中获取数据

links.xml文件

<?php

error_reporting(E_ALL);

$host       = "localhost";
$user       = "root";
$pass       = "smogi";
$database   = "project";


$SQL_query = "SELECT * FROM patient WHERE fname = ???? OR lname = ????";

?>

<pages>
    <link>
        <title>Also display here the name of the user</title>
        <url>members2.php?view=?????</url>
    </link>
</pages>

此处还显示用户的名称
members2.php?视图=?????
无论我在哪里,都意味着我不知道在那里写什么。也许xml的代码需要更多的代码


您能否帮助我修复xml并使其显示数据库中的结果?您需要编辑的文件是livesearch.php文件。livesearch.php将Links.xml作为数据源读取,在您的情况下,该数据源就是数据库。修改后的livesearch.php如下所示:

<?php
$host       = "localhost";
$user       = "root";
$pass       = "Passw0rd";
$database   = "project";

$db = new PDO("mysql:host={$host};dbname={$database}", $user, $pass);
$stmt = $db->prepare("SELECT * FROM patient WHERE fname LIKE :q OR lname LIKE :q");
$stmt->bindValue(':q', '%'.$_GET['q'].'%');
$stmt->execute();

while ( $row = $stmt->fetchObject() ) {
    echo '<a href="members2.php?view=' . $row->username . '" target="_blank">' . $row->fname . ' ' . $row->lname . '</a><br/>';
}
?>


这将产生与w3schools提供的livesearch.php示例类似的输出。

因此,如果我理解您所说的,我只需要search.php和livesearch.php文件。并用您的代码更改livesearch.php文件中的所有代码。对吗?如果我照你说的做,我会得到以下错误
致命错误:未捕获的异常'PDOException',消息'SQLSTATE[HY000][1044]拒绝用户'@'localhost'访问E:\xampp\htdocs\ptixiaki\livesearch.php中的数据库'project''。php:7堆栈跟踪:#0 E:\xampp\htdocs\ptixiaki\livesearch.php(7):PDO->u构造('mysql:host=loca…')#1{main}在第7行的E:\xampp\htdocs\ptixiaki\livesearch.php中抛出
PDO构造函数需要有效的
dsn、用户名、密码
。因此,在您的例子中,
$db=newpdo(“mysql:host={$host};dbname={$database},$user,$pass”)。阅读手册会给你很大帮助!:)@首先感谢您抽出时间。我照你说的做了,现在我得到了这个
致命错误:无法在第9行的E:\xampp\htdocs\ptixiaki\livesearch.php中通过引用传递参数2
,第9行是这个
$stmt->bindParam(':q','%.$\u GET['q'].%')您可能需要在查询中设置两个不同的参数,而不是一个。因此,第一个
:q
应该变成
:q1
,第二个
:q2
。然后通过执行
$stmt->bindParam(':q1','%'.$\u GET['q'].%')
$stmt->bindParam(':q2','%'.$\u GET['q'].%')
,将值绑定到这些参数。是的,类似的操作应该会起作用。尝试将
bindParam
替换为
bindValue