duan205571 2014-06-22 02: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-22 02: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.

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改