简体   繁体   English

从自动完成的AJAX JQuery Mobile调用PHP脚本

[英]Call PHP Script from Auto-complete AJAX JQuery Mobile

I have following auto-complete code to call PHP file via AJAX and get the results in JSON form: 我有以下自动完成的代码,可通过AJAX调用PHP文件并以JSON格式获取结果:

$( "#autocomplete_customer" ).on( "listviewbeforefilter", function ( e, data ) {
    var $ul = $( this ),
        $input = $( data.input ),
        value = $input.val(),
        html = "";
    $ul.html( "" );
    if ( value && value.length > 0 ) {
        $ul.html( "<li><div class='ui-loader'><span class='ui-icon ui-icon-loading'></span></div></li>" );
        $ul.listview( "refresh" );
        $.ajax({
            url: "SearchCustomer.php",
            dataType: "jsonp",
            crossDomain: true,
    data: {
                term: $input.val()
            }

        })
        .then( function ( response ) {
            $.each( response, function ( i, val ) {
                html += "<li>" + val + "</li>";
            });
            $ul.html( html );
            $ul.listview( "refresh" );
            $ul.trigger( "updatelayout");
        });
    }
});

Here is SearchCustomer.php file: 这是SearchCustomer.php文件:

<?php
include "config.php";

mysql_select_db($db, $con);
$search = $_GET['term'];    
$result = mysql_query("my SELECT query on basis of $search") or die('Something went wrong');
$json = '[';
    $first = true;
    while ($row = mysql_fetch_assoc($result))
    {
        if (!$first) { $json .=  ','; } else { $first = false; }
        $json .= ".$row['cli_razon_social'].";
    }
    $json .= ']';
    echo $json;
?>

I am facing two issues: 我面临两个问题:

  1. Results are not returning from PHP file or I am doing it wrong. 结果没有从PHP文件返回,或者我做错了。 Please help me to fix it. 请帮助我修复它。

  2. Currently user cannot select a value from auto-complete list. 当前,用户无法从自动完成列表中选择一个值。 How can I modify this script to let user select auto-complete value from text field auto-complete ul list? 如何修改此脚本,以允许用户从文本字段自动完成ul列表中选择自动完成值?

Thanks. 谢谢。

You need to prepare a json object to send from your PHP file. 您需要准备一个json对象,以便从您的PHP文件发送。

Take a look at json_encode() . 看看json_encode()

Ideally, you would prepare an associative array and then convert it to a JSON 理想情况下,您将准备一个关联数组,然后将其转换为JSON

For example 例如

$arr = array();
$arr['name'] = 'PHP';
echo json_encode($arr);

Output 产量

{
    'name' : 'PHP'
}

You can then refer to the name in your JS as 然后,您可以将JS中的名称引用为

.then(function(resp) { console.log(resp.name); }

This will output PHP in the console. 这将在控制台中输出PHP

However, you might be better off using jQuery UI's built in Autocomplete. 但是,使用内置于自动完成功能的jQuery UI可能会更好。 Please refer to this post . 请参考这篇文章

Moreover, your code is vulnerable to SQL injections. 此外,您的代码容易受到SQL注入的攻击。 You should switch to Prepared Statements to avoid this. 您应该切换到“预备语句”以避免这种情况。

UPDATE : 更新:

As for your second issue, you can first assign a class to the list items, eg <li class="autocomp-data"> and then bind a click handler to it. 至于第二个问题,您可以先为列表项分配一个类,例如<li class="autocomp-data"> ,然后将一个单击处理程序绑定到<li class="autocomp-data">

$ul.html( html );
$('.autocomp-data').click(function() {
    $('correct-selector').val($(this).text());
});

Please note that depending on where you want to assign the text, you might need to use either .val() or .html() . 请注意,根据您要分配文本的位置,可能需要使用.val().html()

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

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