简体   繁体   English

来自数据库国家/地区的jQuery自动完成

[英]Jquery autocomplete from database countries

I'm having trouble with completing an autocomplete function in Jquery with data from database. 我在用数据库中的数据完成Jquery中的自动完成功能时遇到麻烦。 Whenever I type something I always get 'no results found' message. 每当我键入内容时,我总是收到“未找到结果”消息。 I want it to display a country name as the user is typing. 我希望它在用户输入时显示国家名称。

jquery.php 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>

This is my suggested.php page: 这是我的proposal.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();
}
?>

In the database I have a table named countries and within it countryid, countryname and countryflag. 在数据库中,我有一个名为国家的表,表中有countryid,countryname和countryflag。 I need to extract only the countryname column. 我只需要提取countryname列。 I've tried using $_GET['term'] and $_REQUEST['term']. 我尝试使用$ _GET ['term']和$ _REQUEST ['term']。 I've tried different tutorials but none of them seems to work. 我尝试了不同的教程,但似乎都没有用。 If you have any suggestions please tell me. 如果您有任何建议,请告诉我。 Thank you 谢谢

It will be delay your execution if you run query in every time when you type,because you can input value as a query condition value! 如果您每次键入查询都在运行,则会延迟执行,因为您可以输入值作为查询条件值! 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

This can reduce your page delay respond time by sql query running 这可以通过运行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>

suggested.php proposal.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();
}
?>

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

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