如何从自动完成获取类别的ID

时间:2013-05-04 06:04:03

标签: php ajax

我有输入类型,可以任何用户插入类别名称,这里当用户类型类别我需要使用类别名称获取此类别的ID并将其放入另一个隐藏的文本类型

我的autocomplate的ajax代码: -

$("#txtCompanyCat").autocomplete("get_cat_list.php", {
    width: 260,
    matchContains: true,
    //mustMatch: true,
    //minChars: 0,
    //multiple: true,
    //highlight: false,
    //multipleSeparator: ",",
    selectFirst: false
});

和get_cat_list.php代码: -

<?php
ob_start();
session_start();
if(file_exists('select.php'))
{
    require_once('function/connect.php');
    $connect = new connect();

$q = strtolower($_GET["q"]);
if (!$q) return;

$sql = "SELECT DISTINCT Category_name FROM category where 
            Category_delete ='0' and Category_name LIKE '%$q%'";
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) {
    $cname = $rs['Category_name'];
    echo "$cname\n";
}
}
?>

这里我得到类别名称并将其插入: -

<input type="text" id="txtSearchCat" class="class_input tooltip_s" name="txtSearchCat" placeholder="Type category ..." title="Type category" >   

我需要类别ID,如何获得

1 个答案:

答案 0 :(得分:1)

试试这个

    while($rs = mysql_fetch_array($rsd)) {
     $cname = $rs['Category_name'];
     $cid = $rs['Category_id'];
      echo "$cname|$cid\n";
     }

 var $j = jQuery.noConflict();
 $j(document).ready(function(){
 var url2='get_cat_list.php';   ////here is path for your file


                                $j('#txtSearchCat').autocomplete(url2, {
                                width: 278,
                                matchContains: true,
                                selectFirst: false
                                });
                             $j("#txtSearchCat").result(function(event, data, formatted) {

                        $j("#txtSearchCatID").val(data[1]);

                });

            });

将id存储在隐藏字段中