doukougua7873
doukougua7873
2015-07-19 00:55

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 dongshiqin1352 6年前

    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();
            }
        }
    });
    
    点赞 评论 复制链接分享

为你推荐