jQueryUI的自动完成PHP和JSON [英] jQueryUI autocomplete with PHP and JSON

查看:108
本文介绍了jQueryUI的自动完成PHP和JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个打算从我的数据库可用条目设置用户的位置,一个简单的搜索框。我试图让jQuery用户界面的自动完成工作,但我遇到了问题。随着JavaScript控制台在Chrome中打开我认为没有从脚本响应,我开始键入

期望的结果是,列出城市,国家城市格式下拉列表自动完成。我在几个职位看着这里SO和修改我的code在多次试图解决这个问题,但没有运气。在此先感谢任何指导/修复。大部分AP preciated。

下面是我的jQuery UI的code(我用了code.jquery.com主办的最新库)

 <脚本类型=文/ JavaScript的>
$(文件)。就绪(函数()
{
    $('#语言环境)。自动完成(
    {
        来源:./state_autocomplete.php
        的minLength:2
    });
});
< / SCRIPT>

下面是我的形式code:

 <表格名称=frm_set_locale方法=邮报行动=/ index2.php>
  <输入类型=文本名称=区域设置ID =区域设置级=文本框>
  <输入类型=提交级=按钮NAME =frm_submit_locale值=搜索>
< /表及GT;

而这里是把信息的mysqli的剧本

 < PHP
包括/includes/dbconn.php//查询来获取可用的位置
    $区域=修剪($ _ GET ['术语']);
    $收益率=阵列();    $ Q =SELECT`city`,`state` FROM`locales`其中`city` LIKE'%$区域设置%。'
    $ R = mysqli_query($ Q $连接);    $ JSON =[;
    $第一= TRUE;    而($行= mysqli_fetch_assoc($ R,$连接)){
        如果(!$第一){
            。$ JSON =,;
        }其他{
            $第一= FALSE;
        }        。$ JSON ={'值':'。$行['城市']。,$行['状态']};
    }    。$ JSON =];    回声$ json的;
?>


解决方案

感谢所有帮助。我参加了一个新的方法来state_autocomplete.php,这里是什么让这对我的工作。真干净。

 < PHP
//数据库连接
    包括/includes/dbconn.php//查询来获取可用的位置
    $区域= $ _GET ['术语'];    $ Q =SELECT`city`,`state` FROM`locales`其中`city` LIKE'%$%区域';
    $ R = mysqli_query($连接,$ Q);    $ city_state =阵列();
    而($行= mysqli_fetch_assoc($ R)){
        $结果= $行[城市],$行['状态']。;
        array_push($ city_state,$结果);
    }
    $ JSON = json_en code($ city_state);
    回声$ json的;
?>

I have a simple search box that is going to set a user's location from available entries in my database. I'm trying to get jQuery UI's autocomplete to work but am running into issues. With the JavaScript Console turned on in Chrome I see no response from the script as I begin to type.

The desired result is a dropdown autocomplete list that lists cities in "City, State" format. I've looked at several posts here on SO and have modified my code in many attempts to resolve this issue but no luck. Thanks in advance to any guidance / fixes. Much appreciated.

Here's my jQuery UI code (I am using the most current libraries hosted by code.jquery.com)

<script type="text/javascript">
$(document).ready(function()
{
    $('#locale').autocomplete(
    {
        source: "./state_autocomplete.php",
        minLength: 2
    });
});
</script>

Here's my form code:

<form name="frm_set_locale" method="post" action="/index2.php">
  <input type="text" name="locale" id="locale" class="textbox">
  <input type="submit" class="button" name="frm_submit_locale" value="Search">
</form>

And here's the mysqli script that pulls the information

<?php
include "/includes/dbconn.php";

// Query to get the usable locations
    $locale = trim($_GET['term']);
    $return = array();

    $q = "SELECT `city`, `state` FROM `locales` WHERE `city` LIKE '%".$locale."%'";
    $r = mysqli_query($q, $connect);

    $json = "[";
    $first = true;

    while ($row = mysqli_fetch_assoc($r, $connect)){
        if(!$first){
            $json .=  ",";
        }else{
            $first = false;
        }

        $json .= "{'value':'".$row['city'].", ".$row['state']."}";
    }

    $json .= "]";

    echo $json;
?>

解决方案

Thanks for all the help. I took a new approach to the state_autocomplete.php and here is what got it working for me. Really clean.

<?php
// Database Connection
    include "/includes/dbconn.php";

// Query to get the usable locations
    $locale = $_GET['term'];

    $q = "SELECT `city`, `state` FROM `locales` WHERE `city` LIKE '%$locale%'";
    $r = mysqli_query($connect, $q);            

    $city_state = array();
    while($row = mysqli_fetch_assoc($r)){
        $result = $row['city'].", ".$row['state'];
        array_push($city_state, $result);
    }
    $json = json_encode($city_state);
    echo $json;
?>

这篇关于jQueryUI的自动完成PHP和JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆