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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题