AJAX / Live Search问题

时间:2015-11-28 18:45:26

标签: javascript php ajax

以下是我的文件:

后端搜索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“自动填充”功能,而其他人则执行。它总是相同的,所以如果搜索以不同的方式完成并不重要。我能想到的唯一可能导致问题的是我的数据库中的“问题”字段可以有html元素,但我用htmlspecialchars()函数修复了这个问题。在我将它切换到POST方法之前,我正在使用GET方法,我会使用相同的搜索结果拉出相同的页面并查看代码,这一切都是正确的,即使是那些不执行该功能的那些。我把它切换到POST方法,看它是否会有所作为,但它是完全相同的问题。 Chrome和IE中也出现同样的问题。我究竟做错了什么?或者我应该做些什么。

1 个答案:

答案 0 :(得分:0)

我建议你使用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);