单独的json编码数组结果

时间:2013-10-28 04:22:25

标签: php jquery ajax json

以下php块搜索并返回名称,我想选择一个项目,并且在ajax中能够获取“name”旁边所选行中的其他项目。我如何用ajax分隔它们?

<?php
require_once '../php/db_conx.php';
$req = "SELECT name "
    ."FROM profiles "
    ."WHERE name LIKE '%".$_REQUEST['term']."%' "; 
$query = mysql_query($req);
while($row = mysql_fetch_array($query))
{
    $results[] = array('label' => $row['name']);
}
echo json_encode($results);

?>

这是ajax。

$(function() {
        $( "#SearchInput").autocomplete({
source: 'Search.php',
minLength: 1,
select: function(event, ui) {
$('#Name').append(ui.item.label);
            }
        });
    });

2 个答案:

答案 0 :(得分:0)

您可以使用_renderItem method

_renderItem: function( ul, item ) {       
    $( "#selector" ).text( item.name );// let name is in json
}

或者您也可以select event中的name获取

select: function(event, ui) {
   $('#Name').append(ui.item.label);
   $( "#selector" ).text( item.name );// let name is in json
}

答案 1 :(得分:0)

试试这个:

I think you need to parse data from array.

var contact = JSON.parse(jsontext);
document.write(contact.surname + ", " + contact.firstname);

Use the variable name you have used. 
Read More on: http://msdn.microsoft.com/en-us/library/ie/cc836466(v=vs.94).aspx
  • 感谢