使用AJAX进行实时搜索

时间:2015-04-29 02:13:55

标签: javascript php ajax search

我需要建立一个实时搜索,但我不太了解ajax和javascript,但我理解html,css和php。有人告诉我我做错了什么?

<script>
    function liveSearch(str) {
        if (str == "") {
            document.getElementById("txtHint").innerHTML = "";
            return;
        }
        else { 
            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("txtHint").innerHTML = xmlhttp.responseText;
                }
            }

            xmlhttp.open("GET", "getuser.php?q=" + str, true);
            xmlhttp.send();
        }
    }
</script>

<input type="text" onkeypress="liveSearch(this.name)" name="searchWord"/>

<?php
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';
    $dbname = 'ezcart';
    $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

    if ($conn->connect_error) {
        die('Could not connect to the database: ' . $conn->connect_error);
    }

    $word = $_GET['q'];
    $sql= "SELECT prodNam FROM product WHERE prodNam LIKE '$q' ORDER BY prodNam ASC";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo "<div><p>".$row[prodNam]."</p></div>";
        }
    }
    $conn->close();
?>

2 个答案:

答案 0 :(得分:0)

更改查询

$word = $_GET['q'];

$sql= "SELECT prodNam FROM product WHERE prodNam LIKE '%".$word."%'";

有关详细信息:http://www.w3schools.com/sql/sql_like.asp

答案 1 :(得分:0)

首先你需要生成一个&#34; onkeyup&#34;输入中的事件可以调用您的javascript函数。

示例:

 <input id="search" name="search" type="text" class="form-control" placeholder="Busca alguna empresa" onkeyup="dinamic(this.value)" autocomplete="off"/>

在这里,您将显示结果

                <div class="resultados " >

                <table class="table hidden" id="resultados" name="resultados">




                </table>

    </div>

现在让我们写js函数:

function dinamic(cadena) { //here cadena is your input value
var min = 3;
if (cadena.length >= min) { // true when the typed value have 3 or more characters
    $.ajax({
        type: 'POST',  //method to pass the information to live_search.php
        url: 'live_search.php',
        data: 'cadena=' + cadena, //name of the post variable
        success: function (respuesta) { 
            $('#resultados').html(respuesta); //place to write results
        }
    });
}
if (cadena.length>min) { //will show or not the table depending on the input value
    document.getElementById('resultados').className = 'table';
} else
    document.getElementById('resultados').className += ' hidden';

}

放开php:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'ezcart';
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

if ($conn->connect_error) {
    die('Could not connect to the database: ' . $conn->connect_error);
}

$word = $_POST['cadena'];
$sql= "SELECT prodNam FROM product WHERE prodNam LIKE '%".$word."%' ORDER BY prodNam ASC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
        echo "<div><p>".$row[prodNam]."</p></div>";
    }
}
$conn->close();

请记住,在我的示例中,您的php文件名为live_search.php

我建议您使用PDO而不是程序性的PHP

如果你想我可以用pdo做个例子。