【问题标题】:Jquery autocomplete from database countries来自数据库国家/地区的 Jquery 自动完成
【发布时间】:2016-06-02 19:39:10
【问题描述】:

我在使用数据库中的数据完成 Jquery 中的自动完成功能时遇到问题。每当我输入某些内容时,我总是会收到“未找到结果”的消息。我希望它在用户输入时显示国家名称。

jquery.php

  <?php
require "database/database.php";
session_start();
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
         <link type="text/css" rel="stylesheet" href="CSS/style.css">
         <script type="text/javascript" src="jquery/jquery-1.11.3.min.js"></script>
        <script src="jquery/jquery-ui.min.js"></script>
    </head>
    <body>
        <form action="" method="post">
            <p><label>Country:</label><input type='text' name='country' value='' class='auto'></p>
        </form>

        <script type="text/javascript">
            $(document).ready(function () {
                $('.auto').autocomplete({
                    source:"suggested.php",
                    MinLength: 2
                });
            });

        </script>
    </body>
</html>

这是我建议的.php 页面:

   <?php
require "database/database.php";
$term = trim(strip_tags($_GET['term'])); 
$data = mysql_query("SELECT * FROM countries WHERE countryname LIKE '%".$term."%'");
    while($row = mysql_fetch_assoc($data)) {
        $data[] = array(
        'label' = $row['countryname'],
        'value' = $row['countryname']
        );
    }
    echo json_encode($data);
    flush();
}
?>

在数据库中,我有一个名为 countries 的表,其中包含 countryid、countryname 和 countryflag。我只需要提取 countryname 列。 我试过使用 $_GET['term'] 和 $_REQUEST['term']。 我尝试了不同的教程,但它们似乎都不起作用。 如果您有任何建议,请告诉我。 谢谢

【问题讨论】:

    标签: php jquery database autocomplete jquery-autocomplete


    【解决方案1】:

    如果每次键入时都运行查询会延迟您的执行,因为您可以输入值作为查询条件值! I can give you some I tested logic is run your query first when document is loaded and store as a json data type,then you can set source data in autocomplete

    这可以通过运行 sql 查询来减少页面延迟响应时间

         <form action="" method="post">
            <p><label>Country:</label><input type='text' name='country' value='' class='auto'></p>
        </form>
    
        <script type="text/javascript">
            $(document).ready(function () {
                var source = [];
                $.ajax({
                      url : "suggested.php",
                      data : "GET",
                      dataType : "json",
                      success : function(data){
                           for(var i in data){
                               source.push(data[i]);
                            }
                      }
                     });
                $('.auto').autocomplete({
                    source: source,
                    MinLength: 2
                });
            });
    
        </script>
    

    建议的.php

       <?php
       require "database/database.php";       
       $data = mysql_query("SELECT * FROM countries");
       $result = array();
       while($row = mysql_fetch_assoc($data)) {
        $result[] = array(
        'label' = $row['countryname'],
        'value' = $row['countryname']
        );
    }
    echo json_encode($result);
    flush();
    }
    ?>
    

    【讨论】:

    • 嘿大卫,谢谢你的建议。但是我仍然得到相同的结果(“未找到结果”)。
    • 如何以及在哪里可以检查它?
    • 来自浏览器开发者工具,你可以在网络标签上看到它
    • 我试过只运行建议的.php,似乎有一个错误。我会调查一下。
    • suggesed.php 现在没有任何错误,但我仍然设法得到我正在寻找的结果。每次我开始输入时都会收到“没有搜索结果”。
    猜你喜欢
    • 1970-01-01
    • 2011-12-25
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 2014-10-22
    • 2012-10-22
    相关资源
    最近更新 更多