doukougua7873 2015-07-19 00:55
浏览 143
已采纳

jQuery UI自动完成显示“无搜索结果”

I want to display suggestions from mysql database using jQuery UI Autocomplete

I have following form

 <form action="search.php" method="POST">
        <input type="text" name="search" id="search-input">
        <input type="submit" value="Submit" id="submit">
 </form>

search.php

    <?php 
require_once 'db.php';

$a = array();

    if (isset($_POST['search']) && !empty($_POST['search'])) {
        $search_param = trim($_POST['search']);

        $slct_search = $db->prepare("SELECT student_name FROM student_details WHERE student_name LIKE ?") or die($db->error);
        $slct_search->bind_param('s', $search_param);
        $slct_search->execute();        
        $res = $slct_search->get_result();  
        if($res->num_rows) {            
            while ($result = $res->fetch_object()) {
                $a[] = $result->student_name;
            }
            echo json_encode($a);
        } else {
            echo 'OOPS we had a problem';
        }   
    }
?>

The search.php working fine. it returns

["ravi","ravi"]

JS Code

    $(function() {
      $("#search-input").autocomplete({
        source: "search.php",
        minLength: 2
      }); 
    });

The Problem is when I start type in text box immediately displayed

No search results.

i also tried JQuery UI Autocomplete Search Results do not display

  • 写回答

1条回答 默认 最新

  • dongshiqin1352 2015-07-26 11:26
    关注

    HTML

      <form action="" method="">
            <input type="text" name="search" id="search-input" autocomplete="off">
            <input type="submit" value="Submit" id="submit">
            <div id="empty-message"></div>
      </form>
    

    Now search.php

        $searchTerm = trim($_GET['term']);
    
        $query = $db->query("SELECT student_name FROM student_details WHERE student_name LIKE '%".$searchTerm."%' ORDER BY student_name ASC");
    
    
        while ($row = $query->fetch_object()) {
            $data[] = $row->student_name;
        }
    
        echo json_encode($data);
    

    jquery ui autocomplete only working with $_GET

    So i am using $_GET['term'], See below Picture

    enter image description here

    JS code

    $('#search-input').autocomplete({
        source: 'search.php',
        minLength: 2,
        response: function(event, ui) {
            // ui.content is the array that's about to be sent to the response callback.
            if (ui.content.length === 0) {
                $("#empty-message").text("No results found");
            } else {
                $("#empty-message").empty();
            }
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP