drti52047 2014-12-22 07:41
浏览 81

在Bootstrap中使用typeahead

Im new to bootstrap. I want to use the built-in bootstrap-typeahead.js file for my web application to automatically display the options as the user types into the textbox. I've downloaded the sample code that uses typeahead plugin. It works ok just setting the data to be static. But I want all the data to be dynamically fetch from my database of the table named 'payees' as shown below :

Table: payees
+----------+--------------+---------------+
| payee_id | payee_name   | payee_contact |
+----------+--------------+---------------+
|        1 | John Smith   | 09045544211   |
|        2 | Chen Lou     | 09093457851   |
+----------+--------------+---------------+

I want to display the lists of payee_name from the textbox as the user type using typahead. I've followed some of the tutorials from the net on how to fetch the data from the database using jquery ajax. As of now, I've been trying to display the data from my database using this two files named autocomplete.php and data.php

and heres my code I've been working on :

autocomplete.php

<input type="text" class="typeahead" data-provide="typeahead" autocomplete="off" />

<script src="js/jquery-1.8.0.min.js" type="text/javascript"></script>`
<script src="js/bootstrap-typeahead.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function() {
    $('input.typeahead').typeahead({
        source: function (query, process) {
        $.ajax({
            url: 'data.php',
            type: 'POST',
            dataType: 'JSON',
            data: 'query=' + query,
            success: function(data) {
                console.log(data);
                process(data);
            }
        });
        }
    });
});
</script>

data.php

<?php

$mysqli = new mysqli("localhost", "root", "", "disbursements");

$query = 'SELECT payee_name FROM payees';

if(isset($_POST['query'])){

// Now set the WHERE clause with LIKE query
$query .= ' WHERE payee_name LIKE "%'.$_POST['query'].'%"';
}

$return = array();

if($result = $mysqli->query($query)){
// fetch object array
   while($obj = $result->fetch_object()) {
      $return[] = $obj->payee_name;
   }
   // free result set
   $result->close();
}

// close connection
$mysqli->close();

$json = json_encode($return);
print_r($json);
?>

But that codes doesn't work as it seems like that my php script is not returning any data to display in my textbox. Can someone help me with what is wrong with my codes? thanks

  • 写回答

1条回答 默认 最新

  • dongshi1424 2014-12-22 08:38
    关注

    Try this:

    $('.typeahead').typeahead({
        source: function (query, process) {
            return $.get('data.php', { query: query }, function (data) {
                return process(data.options);
            });
        }
    });
    
    
    
    <?php
        $mysqli = new mysqli("localhost", "root", "", "disbursements");
        $query = 'SELECT payee_name FROM payees';
    
        if (isset($_POST['query'])) {
            // Now set the WHERE clause with LIKE query
            $query.= ' WHERE payee_name LIKE "%' . $_POST['query'] . '%"';
        }
    
        $return = array();
    
        if ($result = $mysqli->query($query)) {
            // fetch object array        
            while ($obj = $result->fetch_object()) {
                $return['options'][] = $obj->payee_name;
            }       
            // free result set        
            $result->close();
        }
    
        // close connection        
        $mysqli->close();
        $json = json_encode($return);
        echo $json;
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog