duan205571 2014-06-21 18:14
浏览 25
已采纳

自动完成中没有输出

I use autocomplete to achieve a Google like search where the search suggestions are in a dropdown while i type what i'm searching for.

The Output of my code

enter image description here

HTML

    <td width="155"  bgcolor="#999999">Client Name</td>
    <td width="218" bgcolor="#999999"><input type="text" name="clientname" id="clientname" class="forinput" /></td>

script

   <script type="text/javascript">
      $(document).ready(function() {
  $( "#clientname" ).autocomplete(
   {
     source:"getautocomplete.php",
     minLength:1
    })
       });
    </script>

getautocomplete.php

  ..databaseconnection
  $term = trim(strip_tags($_GET['term']));//retrieve the search term that autocomplete sends

  $qstring = "SELECT clientname FROM client WHERE clientname LIKE '%".$term."%'";
  $result = mysql_query($qstring);//query the database for entries containing the term

  while ($row = mysql_fetch_array($result,MYSQL_ASSOC))//loop through the retrieved values
   {
    $row['clientname']=htmlentities(stripslashes($row['clientname']));
    $row_set[] = $row;//build an array
   }
   echo json_encode($row_set);//format the array into json data

What i want to achieve
enter image description here

I check my database connection and its correct. Can anybody explain to me what I'm doing wrong? did i missed something?

展开全部

  • 写回答

1条回答 默认 最新

  • duan0504 2014-06-21 18:24
    关注

    I don't think the result you are sending back is valid according to what jQueryUI is expecting.

    Now you are building an array of arrays and you should only send the values back (assuming that the label and the value are the same, the name of the client):

    // $row['clientname']=htmlentities(stripslashes($row['clientname']));
    $row_set[] = htmlentities(stripslashes($row['clientname']));    //build an array
    

    Also note the comments about the deprecated mysql_* functions and sql injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部