簡體   English   中英

使用AJAX進行實時搜索

[英]Live search with AJAX

我需要建立實時搜索,但是我對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();
?>

更改查詢

$word = $_GET['q'];

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

有關更多信息: http : //www.w3schools.com/sql/sql_like.asp

首先,您需要在輸入中生成“ onkeyup”事件以調用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做一個例子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM