首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从自动完成文本框存储选定值id的php代码

从自动完成文本框存储选定值id的php代码
EN

Stack Overflow用户
提问于 2015-04-03 11:46:30
回答 2查看 2.7K关注 0票数 0

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

代码工作正常..。

,但我也需要将选定值的id存储在隐藏的文本框..

代表EX =

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

代码语言:javascript
复制
id   societyname
7      raj
15     lucky

如果我从上面的值中选择raj,那么在隐藏的文本框中显示raj :7的id。

请大家帮帮我。

自动完成文本框

代码语言:javascript
复制
<input id="society_name" name="society"/>
 <input type="hidden" id="society_name" name="societyid"/>

In ajax.php

代码语言:javascript
复制
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

代码语言:javascript
复制
$('#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        
              });
EN

回答 2

Stack Overflow用户

发布于 2015-04-03 11:51:28

在输入上使用val()函数,从ajax结果传递id

代码语言:javascript
复制
$('#society_name').val(data.id);

更新代码:

php:

代码语言:javascript
复制
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);
}

联署材料:

代码语言:javascript
复制
  $('#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        
                  });
票数 0
EN

Stack Overflow用户

发布于 2015-04-03 12:19:53

如果您需要ID,那么从DB中获取它们并在ajax调用中返回(我想这就是您正在讨论的ID)。从这里开始,您可以使用类似这样的方法来缓存返回的结果,例如关联数组:$arrvalue = id;如果用户选择其中一个值,您可以获得相应的ID。

顺便说一下。对未来的一些建议: 1. id的字段应该具有唯一的值。2.您应该考虑保护查询不受sql攻击。在这里,您甚至不转义由用户提供的字符串。3.据我所知,mysql已经过时,所以尝试使用mysqli。

ajax.php

代码语言:javascript
复制
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文件

代码语言:javascript
复制
$.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
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29431608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档