繁体   English   中英

AJAX,jQuery Ud / PHP:POST方法自动完成未显示

[英]AJAX, JQuery Ud / PHP: POST Method Autocomplete not showing up

我当前正在使用具有“自动完成”功能的init创建一个Wordpress模板,该代码如下。 目前,我的自动填充功能无法正常工作,并且什么也没有显示。 检查系统,它给了我...

 Uncaught ReferenceError: request is not defined

我最近有一个自动完成功能,但是无法使用自动完成功能键盘上下,输入功能。 它可以使用鼠标,但不能使用键盘功能。 它位于wordpress模板上,目前仅由鼠标控制。 另一个关键因素是它是“ POST”而不是“ GET”。 另一个关键因素是我确实有一个隐藏的输入字段,其ID称为“搜索ID”。 隐藏的“ search-id”输入字段将与id一起更新并发送到服务器,而“ search-box”输入字段仅向用户显示完整说明,但是如果该值等于我们发送的只是ID。 可以在HTML和PHP中看到...请帮我为什么我的自动完成功能不起作用?

jQuery UI / AJAX

    <script>
        $(document).ready(function(){
            $("#search-box").autocomplete({
                minLength: 2,
                source: function(request, response){
                    $.ajax({
                        type: "POST",
                        url: "autocomplete.php",
                        data:'keyword=' + req.term,
                        success: response,
                        dataType: 'json',
                        minLength: 2,
                        delay: 100

                    });
                },
                select: function(event, ui){
                    $("#search-box").val(ui.item.label);
                    $("#search-id").val(ui.item.value);
                    return false;
                }
            });
        });
        /* function selectCountry(val) {
          $("#search-id").val(val);
          $("#suggesstion-box").hide();
        }
        function updateSearchBox(el) {
          $("#search-box").val($(el).html());   
        } */
    </script>

HTML

<form role="search" name="remarks_lookup" id="myForm" method="post" class="search-form" action="remarks-look-up" >
    <div style="background-color:#ffffff; width:100%; float:left;  padding-bottom:25px; clear:both;  ">
        <div class="frmSearch " style="width:80%; float:left;"   >
            <label>                                             
                <input type="search" name="jurisdiction_search" id="search-box"  placeholder="Enter County, City, or Client Name" autocomplete="off" />
                <input type="hidden" name="search" id="search-id"  />
                <div class="box" id="suggesstion-box"></div>
            </label>
        </div>
        <div style="width:20%; float:left; clear:right;">
            <label>                                         
                <input style="width:100%; height: 50px;" type="submit"  value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" />
            </label>
        </div>
    </div>
</form> 

Autocomplete.php

<?php
include_once "functions.php";

     if(isset($_POST['keyword']))
    {
        $database = lookup_connectToDatabase();                     
        $result = pg_query($database, "SELECT country, id, state FROM customers WHERE country iLIKE '" . $_POST["keyword"] . "%' OR id iLIKE '" . $_POST["keyword"] . "%' ORDER BY country");

        if (!$result){
                echo "<div class='show' align='left'>No matching records.</div>";
        }else {

            while($row=pg_fetch_assoc($result)){

                $array[] = array(
                    'label' = $row['id'].': '.$row['country'].', '.$row['state'],
                    'value' = $row['id'],
                );

            } 
            echo json_encode ($array);
        }
    }                                                   
?>

总的来说,我想完成的工作是能够使用键盘功能(例如上下箭头,甚至在建议框上按Enter,然后让search-id输入字段显示说明,他们必须按Enter再次,它将搜索ID发送到服务器。

过去一个月,我一直在努力解决此问题,以期找出解决自动完成功能的不同方法。 我是JSON,JQuery-UI和Ajax的新手。 我确实需要任何人的帮助才能在我的网站上完成此功能。 请帮忙!

如果您愿意提供帮助并希望仔细查看代码甚至进行测试,请告诉我。 我需要任何帮助以完成此自动完成功能。

想通了...。

jQuery UI

<script>
$(document).ready(function(){
        $("#search-box").autocomplete({
        minLength: 2,
        source: function(request, response){
            $.ajax({
                type: "POST",
                url: "autocomplete.php",
                dataType: "json",
                data:'keyword=' + request.term,
                success: function(data){
                    response(data);
                }
            });
        },
        select: function(event, ui){
            $("#search-box").val(ui.item.label);
            $("#search-id").val(ui.item.value);
            return false;
        }
    });
});
</script>

HTML

<form role="search" name="remarks_lookup" id="myForm" method="post" class="search-form" action="/VCSWeb/remarks-look-up/" >
    <div style="background-color:#ffffff; width:100%; float:left;  padding-bottom:25px; clear:both;  ">
        <div class="frmSearch " style="width:80%; float:left;"   >
            <label>                                         
                <input   size="59" maxlength="75" type="search" name="jurisdiction_search"  class="search_keyword" id="search-box"  placeholder="Enter Jurisdiction, County, City, or Client Name"  />
                <input type="hidden" name="search" id="search-id"  /> 
            </label>
        </div>
        <div style="width:20%; float:left; clear:right;">
            <label>                                         
                <input class="search_button" style="width:100%; height: 50px;" type="submit"  value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" />
            </label>
        </div>
    </div>
</form>

的PHP

<?php
     if(isset($_POST['keyword']))
    {
        $database = lookup_connectToDatabase();                     
        $result = pg_query($database, "SELECT country, id, state FROM customers WHERE country iLIKE '" . $_POST["keyword"] . "%' OR id iLIKE '" . $_POST["keyword"] . "%' ORDER BY country");

        if (!$result){
                echo "<div class='show' align='left'>No matching records.</div>";
        }else {

            while($row=pg_fetch_array($result)){

                $array[] = array(
                    'label' => '('.$row['id'].') '.$row['country'].', '.$row['state'],
                    'id' => $row['country'].', '.$row['state'],
                    'value' => $row['id'],
                );

            } 
            echo json_encode ($array);
        }
    }                                                   
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM