用于存储自动完成文本框中选定值的id的php代码

用于存储自动完成文本框中选定值的id的php代码,php,jquery,jquery-ui,Php,Jquery,Jquery Ui,下面是我的自动完成文本框代码 代码运行良好 但我还需要将所选值的id存储在隐藏文本框中。 对于EX= 我的自动文本框中有两个值 id societyname 7 raj 15 lucky 若我从上面的值中选择了raj,那个么在隐藏文本框中显示raj ie:7的id 请任何人帮助我 自动完成文本框 <input id="society_name" name="society"/> <input type="hidden" id="society_name

下面是我的自动完成文本框代码

代码运行良好

但我还需要将所选值的id存储在隐藏文本框中。

对于EX=

我的自动文本框中有两个值

id   societyname
7      raj
15     lucky
若我从上面的值中选择了raj,那个么在隐藏文本框中显示raj ie:7的id

请任何人帮助我

自动完成文本框

<input id="society_name" name="society"/>
 <input type="hidden" id="society_name" name="societyid"/>
auto.js

$('#society_name').autocomplete({
                source: function( request, response ) {
                    $.ajax({
                        url : 'ajax.php',
                        dataType: "json",
                        data: {
                           name_startsWith: request.term,
                           type: 'society'
                        },
                         success: function( data ) {
                             response( $.map( data, function( item ) {
                                return {
                                    label: item,
                                    value: item
                                }
                            }));
                        }
                    });
                },
                autoFocus: true,
                minLength: 0        
              });
在输入端使用val()函数从ajax结果传递id

$('#society_name').val(data.id);
更新代码:

php:

js:


如果您需要ID,那么从DB获取它们并在ajax调用中返回(我猜这就是您所说的ID)。 从这一点上,您可以使用类似这样的方法,将返回的结果缓存在关联数组中: $arr[value]=id; 如果用户选择其中一个值,则可以获得相应的ID。 我想这是一种方法

顺便说一句。 未来的一些建议: 1.id的字段应具有唯一的值。 2.应该考虑保护查询不受SQL攻击的影响。在这里,您甚至不会转义用户给定的字符串。 3.据我记忆所及,mysql已经过时了,所以试着改用mysqli

ajax.php

if($_GET['type'] == 'society'){ 
    $result = mysql_query("SELECT society FROM societymaster where society LIKE '".strtoupper($_GET['name_startsWith'])."%'");  
    $data = array();
    while ($row = mysql_fetch_array($result)) {
        array_push($data, $row['society']); 
    }   
    echo json_encode($data);
}
if($_GET['type'] == 'society'){ 
    // try to switch to mysqli. You can also consider prepare statements.  
    $result = mysql_query("SELECT id, society FROM societymaster where society LIKE '".mysql_real_escape_string(strtoupper($_GET['name_startsWith']))."%'");
    $data = array();
    while ($row = mysql_fetch_array($result)) {
        array_push($data, $row); 
    }   
    // mysql_fetch_array returns indexed array so we should get JSON string like: "[['id1', 'value1'], ['id2', 'value2'], ...]"
    echo json_encode($data);
}
.js文件

$.arr = [];
$('#society_name').autocomplete({
    source: function( request, response ) {
        $.ajax({
            url : 'ajax.php',
            dataType: "json",
            data: {
                name_startsWith: request.term,
                type: 'society'
            },
            success: function( data ) {
                // clear cache array and fill it with information about the new results (societies and corresponding ids)
                $.arr = [];
                $.each(data, function (idx, item) {
                    $.arr[item[1]] = item[0];
                });
                $('#society_name').val(data[0].id);
                response( $.map( data, function( item ) {
                    return {
                        label: item,
                        value: item
                    }
                }));
            }
        });
    },
    autoFocus: true,
    minLength: 0        
});

// catch an event when the autocomplete closes and get id for selected society name
$("#tags").on("autocompleteclose", function() {
    // close event does not provide object data so we must fetch it again
    var selectedValue = $("#tags").val();
    var id = $.arr[selectedValue];
    // now you can set this ID as the value of your hidden input
});

始终保持简单,您不能显示隐藏的输入值,您可以将值存储在其中。实际上,我需要将此id存储在数据库中。触发成功事件时,请参阅更新的代码您想要ajax结果的id或其他id?我需要在自动完成文本框中显示名称,但在选择此ajax结果的显示id后..例如-我在我的问题中贴了一个例子…是的,你是对的…但我是ajax新手,现在不知道如何返回和显示id plz代码帮助。ajax是一种用于与服务器异步通信的技术。您在ajax.js中所做的只是使用php编写简单的脚本。如果您想从数据库中获取id,然后将id添加到society“SELECT id,society from…”之前,然后您可以将整行推送到结果数组“array\u push($data,$row)”然后,您将拥有一个二维数组,该数组将被编码为JSON stringyes。我创建了用于数据库搜索的ajax.php文件,以及用于使用javascript和ajax处理请求的auto.js。因为您使用fetch_数组函数,所以我假设我们将得到如下输出:“[['id1',society1'],['id2',society2']…]”因此,在js站点上,您可以像这样修改map函数:$.map(数据、函数(项){return{label:item[1],value:item[0]}),然后尝试使用madalin ivascu建议的val()函数(我对autocomplete组件不太熟悉,所以必须先测试它,然后才能关闭此案例:D)
if($_GET['type'] == 'society'){ 
    // try to switch to mysqli. You can also consider prepare statements.  
    $result = mysql_query("SELECT id, society FROM societymaster where society LIKE '".mysql_real_escape_string(strtoupper($_GET['name_startsWith']))."%'");
    $data = array();
    while ($row = mysql_fetch_array($result)) {
        array_push($data, $row); 
    }   
    // mysql_fetch_array returns indexed array so we should get JSON string like: "[['id1', 'value1'], ['id2', 'value2'], ...]"
    echo json_encode($data);
}
$.arr = [];
$('#society_name').autocomplete({
    source: function( request, response ) {
        $.ajax({
            url : 'ajax.php',
            dataType: "json",
            data: {
                name_startsWith: request.term,
                type: 'society'
            },
            success: function( data ) {
                // clear cache array and fill it with information about the new results (societies and corresponding ids)
                $.arr = [];
                $.each(data, function (idx, item) {
                    $.arr[item[1]] = item[0];
                });
                $('#society_name').val(data[0].id);
                response( $.map( data, function( item ) {
                    return {
                        label: item,
                        value: item
                    }
                }));
            }
        });
    },
    autoFocus: true,
    minLength: 0        
});

// catch an event when the autocomplete closes and get id for selected society name
$("#tags").on("autocompleteclose", function() {
    // close event does not provide object data so we must fetch it again
    var selectedValue = $("#tags").val();
    var id = $.arr[selectedValue];
    // now you can set this ID as the value of your hidden input
});