用于存储自动填充文本框中所选值的ID的php代码

时间:2015-04-03 11:46:31

标签: php jquery jquery-ui

下面的

是我的自动填充文本框代码..

代码工作正常..

但我还需要在隐藏文本框中存储所选值的ID ..

FOR EX =

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

id   societyname
7      raj
15     lucky

好的,如果我从上面的值中选择raj然后显示raj的id,即隐藏文本框中的7

任何人帮助我。

自动填充文本框

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

在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);
}

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        
              });

2 个答案:

答案 0 :(得分:0)

在输入上使用val()函数传递来自ajax结果的id

$('#society_name').val(data.id);

更新代码:

PHP:

if($_GET['type'] == 'society'){ 
    $result = mysql_query("SELECT id,society FROM societymaster where society LIKE '".strtoupper($_GET['name_startsWith'])."%'");  
    $data = array();
    while ($row = mysql_fetch_array($result)) {
        array_push($data,$row); 
    }   
    echo json_encode($data);
}

JS:

  $('#society_name').autocomplete({
                    source: function( request, response ) {
                        $.ajax({
                            url : 'ajax.php',
                            dataType: "json",
                            data: {
                               name_startsWith: request.term,
                               type: 'society'
                            },
                             success: function( data ) {
                                $('#society_name').val(data[0].id);
                                 response( $.map( data, function( item ) {
                                    return {
                                        label: item,
                                        value: item
                                    }
                                }));
                            }
                        });
                    },
                    autoFocus: true,
                    minLength: 0        
                  });

答案 1 :(得分:0)

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

顺便说一句。 一些未来的提示: 1. id的字段应该具有唯一值。 2.您应该考虑保护您的查询免受SQL攻击。在这里,您甚至无法转义用户提供的字符串。 3.据我记得,mysql已经过时,所以请尝试使用mysqli。

<强> ajax.php

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
});
相关问题