用php mysql ajax实时搜索

时间:2017-04-17 17:59:21

标签: php jquery mysql ajax

我尝试使用php mysql ajax

创建实时搜索

输入字段

<input type="text" onkeyup="getProducts(this.value)" class="form-control">

显示搜索结果

 <div id="results"></div>

Ajax googleapis连接

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

功能getProducts

 <script type="text/javascript">
        function getProducts(value) {
            $.post("getProducts.php", {
                    partialState: value
                }, function(data) $("#results").html(data);
            });

        }

    </script>

getProducts.php代码页

include("includes/connect.php");

$partialStates=$_POST['partialState'];

$states=mysqli_query($conn,"
        select 
        p.db_pcode,
        p.db_pname,
        p.db_punit,
        p.db_pprice,
        p.db_pqtyalert,
        p.db_pcategory,
        p.db_psupplier,
        p.db_pdesc,
        p.db_pvat,
        p.db_pdate,
        p.db_corid,
        c.db_ccat as categoryname,
        su.db_sname as suppliername,
        s.db_secname as blockname,
        s1.db_secid as cornerid,
        s1.db_secname as cornername,
        s2.db_secid as sectionid,
        s2.db_secname as sectionname
        from tbl_products as p
        left join tbl_category as c
        on 
        c.db_cid=p.db_pcategory
        left join tbl_suppliers as su
        on
        su.db_sid=p.db_psupplier
        left join tbl_section as s
        on
        s.db_secid=p.db_corid
        join tbl_section as s1
        on 
        s.db_parent=s1.db_secid 
        join tbl_section as s2
        on
        s1.db_parent=s2.db_secid 
        where 
        p.db_pname like '%$partialStates%'
        ")or die(mysqli_error($conn));
while($state=mysqli_fetch_array($states)){
    echo"<div>".$state['db_pname']."</div>";
}

问题是代码不显示任何内容

我尝试打开getProducts.php页面,看看我是否有任何php错误

但没有出现错误且产品已打印

任何人都可以帮忙知道错误在哪里吗? 在控制台中我有这2个错误

未捕获的SyntaxError:意外的标识符

2products.php:124未捕获的ReferenceError:未定义getProducts

1 个答案:

答案 0 :(得分:2)

您遇到语法错误...在{回调

中缺少$.post
$.post("getProducts.php", {partialState: value}, function(data) { 
                                                            // ^^ missing
   $("#results").html(data);
 });