AJAX/实时搜索问题


Issue with AJAX/Live Search

以下是我的文件:

后端搜索PHP文件:

<?php
require "../sinfo.php";
function chk_phone($orig) {
    return preg_replace("/[^0-9]/","",$orig);
}
$s = $_POST["s"];
$sql = "SELECT * FROM requests WHERE id LIKE '%" . $s . "%' OR " .
"lower(firstname) LIKE '%" . strtolower($s) . "%' OR " .
"lower(lastname) LIKE '%" . strtolower($s) . "%' OR " .
"dayphone LIKE '%" . strtolower($s) . "%' ORDER BY id ASC";
$result = $conn->query($sql);
$valid = array();
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        if ($row["status"] == "Complete" && date_compare($row["timestamp"]) < 37) {
            $valid[] = $row;
        }
    }
}
if (count($valid) > 0) {
    echo "<ul>'n";
    foreach ($valid as $row) {
        echo "<li onclick='"javascript: autofill('" . $row["id"] . "', '" . 
        $row["firstname"] . "', '" . 
        $row["lastname"] . "', '" . 
        chk_phone($row["dayphone"]) . "', '" . 
        chk_phone($row["evephone"]) . "', '" . 
        $row["email"] . "', '" . 
        $row["make"] . "', '" . 
        $row["os"] . "', '" . 
        htmlspecialchars($row["issue"]) . "', '" . 
        $row["password1"] . "', '" . 
        $row["password2"] . 
        "');'">" . $row["id"] . " - " . $row["firstname"]. " " . $row["lastname"] . "</li>'n";
    }
    echo "</ul>'n";
} else {
    echo "-- No Results Found --";
}?>

这是javascript文件:

    function upsearch(content) {
       var xmlhttp;
                  try{
                     // Opera 8.0+, Firefox, Safari
                     xmlhttp = new XMLHttpRequest();
                  }
                  catch (e){
                     // Internet Explorer Browsers
                     try{
                        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                     }
                     catch (e) {
                        try{
                           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        catch (e){
                           alert("Something went wrong.!'nError: 'n" + e);
                           return false;
                        }
                     }
                  }
       xmlhttp.onreadystatechange = function() {
       //if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
           document.getElementById("results").innerHTML = xmlhttp.responseText;
           //}
       };
       var params = "s=" + content;
       xmlhttp.open("POST", "ajax/requestsearch.php", true);
       xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
       xmlhttp.send(params);
    }
function autofill(rid, fname, lname, dphone, ephone, email, make, os, issue, password1, password2) {
    document.getElementById("searchrid").value = rid;
    document.getElementById("searchrid").disabled = true;
    document.getElementById("custinfo").style.display = "block";
    document.getElementById("firstname").value = fname;
    document.getElementById("firstname").disabled = true;
    document.getElementById("lastname").value = lname;
    document.getElementById("lastname").disabled = true;
    document.getElementById("dayphone").value = dphone;
    document.getElementById("evephone").value = ephone;
    document.getElementById("email").value = email;
    document.getElementById("equipinfo").style.display = "block";
    document.getElementById("make").value = make;
    document.getElementById("make").disabled = true;
    document.getElementById("password1").value = password1;
    document.getElementById("password2").value = password2;
    document.getElementById("originalissue").style.display = "block";
    document.getElementById("originalissue").innerHTML = issue;
    document.getElementById("os").value = os;
}

在调用后端页面的PHP主页上,我得到了预期的所有结果。它显示我想要的一切。我的问题是,它返回的所有行,其中一些行不执行javascript"autofill"功能,而另一些行执行。它们总是相同的,所以搜索是否以不同的方式进行也没关系。我唯一能想到的可能会导致问题的是,我的数据库中的"issue"字段可以有html元素,但我用htmlspecialchar()函数解决了这个问题。在我把它切换到POST方法之前,我使用的是GET方法,我会用相同的搜索结果调出相同的页面,并查看代码,结果都是正确的,即使是那些不执行该功能的页面。我把它切换到POST方法,看看它是否会有所不同,但这是完全相同的问题。Chrome和IE也出现了同样的问题。我做错了什么?或者我应该做些什么不同的事情。

我建议您使用jQuery。

只需使用简单的ajax请求,如:

$.ajax({
type: 'POST',
url: 'ajax/requestsearch.php',       
data: 'q='+query,              
success: function(data){
    var r = $.parseJSON(data);                         
    autofill(r[0].lastname,r[0].firstname, r[0].phone, etc);
}
});

和requestsearch.php

    $q = $_POST['q'];
    $sql="SELECT * FROM DataBase WHERE firstname LIKE '%$q%' ORDER BY firstname asc";
         $result = $conn->query($sql);             
         while($row = $result->fetch_assoc()){
              $data[] = $row;
         }
         echo json_encode($data);