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

问题描述:

I did a search on web and i found on w3schools this link that tells you how to make a live search with Php, Ajax and XML (link). I can understand what they are doing on their code which is the below...

The search.php file

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

and the livesearch.php file

<?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;
?> 

But what they do next is to have an xml page (link) that contains all the data they want to search but in my case I want to search my database table and I am using SQL. I tried to do some coding but I cannot find how I get the data from the query .

The links.xml file

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

Wherever i have ???? means that I dont know what to write there. Maybe the code of the xml need more code.

Can you help me to fix my xml and make it display results from my database

The file that you would need to edit is the livesearch.php file. Links.xml is read by livesearch.php as a data source, which in your case would be the database. The modified livesearch.php would look something like the following:

<?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/>';
}
?>

This will produce similar output to the livesearch.php example provided by w3schools.