jquery自动完成返回数据

时间:2016-01-24 02:36:07

标签: php jquery json autocomplete

这是我的第一页。以下是自动填充功能:

      <script>
          $(function() {
            $("#skills").autocomplete({
              source: 'search.php', 
                  minLength:1,
            select: function(event,ui){ *//autocomplete*
              var code = ui.item.id;
              if(code != '') {

              }
            }
            });
          });
          </script>

我的search.php页面

  $query = $db->query("SELECT *       
                         FROM patient  
                         WHERE FirstName LIKE '%$searchTerm%'  
                         OR MiddleName LIKE '%$searchTerm%'  
                         OR LastName LIKE '%$searchTerm%'  
                         OR PatientId LIKE '%$searchTerm%'");
    while ($row = $query->fetch_assoc()) {
        $data[] = $row['FirstName']." ".$row['MiddleName']." ".$row['LastName'];
        $id = htmlentities(stripslashes($row['PatientId'])); *//i want to pass this value*
    }
    //return json data
    echo json_encode($data);

我使用了jQuery自动完成功能。我想将$id值传递回第1页。我怎么能通过呢?有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

只需添加一个包含每个项目的标签和值的数组

$label = $row['FirstName']." ".$row['MiddleName']." ".$row['LastName'];
$value = htmlentities(stripslashes($row['PatientId'])); 
$data[] = array('label' => $label, 'value' => $value);