dsvs50005 2013-06-06 18:43
浏览 52
已采纳

使用jQuery-ui使用php和javascript自动完成文本框

I am trying to autocomplete a text box in my search.php code using autocomplete.php

I know that my php code works perfectly and echo's back exactly what is needed for the autocomplete function in jQuery.

Here is the html for the text box.

<input type="text" name='search' id="search" class="input-block-level" autocomplete="off" placeholder="search...">

Here is my script for the autocomplete function

<script>
  jQuery(document).ready(function($){
    $('#search').autocomplete({source:'autocomplete.php', minLength:2});
  });
</script>

Here is the php file

<?php


 if ( !isset($_GET['term']) )
exit;


$conn = odbc_connect('Abilis', 'infosysreader', 'Wilsons12'); 

$query = "SELECT TOP 10 [Del_ad1] FROM [Abilis].[dbo].[Customers] WHERE Del_ad1 LIKE    '%".$_GET['term']."%'";

$rs = odbc_exec($conn, $query);



$data = array();

for($i = 0; $i<odbc_num_rows($rs);$i++){
$row = odbc_fetch_array($rs, $i);

$data[] = array(
            'label' => $row['Del_ad1'],
            'value' => $row['Del_ad1']
);
}

// jQuery wants JSON data
echo json_encode($data);
flush();

Edit:

I found my error at the end of my html file. It was just a mistake on my part, the method I use above works fine.

  • 写回答

1条回答 默认 最新

  • douyan2470 2013-06-06 18:54
    关注

    Not sure what your problem is but since your PHP correctly returns json encoded string then problem is with autocomplete call. Try this and let me know if it makes any difference:

    $('#search').autocomplete({
       minLength:2,
       source: function(request, response) {
           $.ajax({
             url: 'autocomplete.php', 
             dataType: 'json',
             data: { term : request.term },
             success: function(result) {
                  response(result);
             }
           });
       }
    });
    

    Also try changing autocomplete="off" to autocomplete="on"

    Remove the following from input element:

    class="input-block-level" autocomplete="off" placeholder="search..."
    

    and try with <input type="text" name='search' id="search" />

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

报告相同问题?

悬赏问题

  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测