简体   繁体   English

自动完成TextBox JQuery PHP MySQL

[英]Auto Complete TextBox JQuery PHP MySQL

I have been referring to Auto Complete Text Box using JQuery, PHP, MySQL 我一直指的是使用JQuery,PHP,MySQL的Auto Complete Text Box

My page is split into various PHP Templates. 我的页面分为各种PHP模板。 Below is the code: 以下是代码:

Header.php header.php文件

<!-- JQuery AutoComplete CSS -->
<link href="../css/jquery.autocomplete.css" rel="stylesheet">

<!-- JQuery JS -->
<script type="text/javascript" src="../js/jquery.js"></script>

<!-- JQuery AutoComplete JS -->
<script type="text/javascript" src="../js/jquery.autocomplete.js"></script>

Main Page This page exists inside http://localhost/booking/mainpage.php 主页面此页面位于http://localhost/booking/mainpage.php中

<tr>
<td class="col-md-4"><label class="control-label">Pooja Name</label></td>
<td class="col-md-8"><input type="text" name="txtPoojaName" id="poojaName" class="form-control" placeholder="Enter Pooja Name"></td>
</tr>

I have included the script code in the Main Page at the very end before the ending of the body tag. 我已经在主体标签结束前的最末端将脚本代码包含在主页面中。

 <script>
 $(document).ready(function(){
  $("#poojaName").autocomplete("autocomplete.php", {
        selectFirst: true
  });
 });
</script>

autocomplete.php autocomplete.php

This page exists inside http://localhost/booking/autocomplete.php 此页面存在于http://localhost/booking/autocomplete.php中

    <?php
    $q=$_GET['q'];
    $my_data=mysql_real_escape_string($q);
    $dbc=mysqli_connect('localhost','root','pass@1234','srkbs') or die("Database Error");
    $sql="select distinct poojaname from v_poojadetails where poojaname like '%$my_data%' order by poojaname";
    $result = mysqli_query($dbc,$sql) or die(mysqli_error());
    //echo mysqli_num_rows($result);
    if($result)
    {
        while($row=mysqli_fetch_array($result))
        {
            echo $row['PoojaName']."\n";
        }
    }
?>

I have tested the autocomplete.php separately and it returns the result set. 我已经单独测试了autocomplete.php并返回了结果集。 I guess I am making some mistake in JQuery or some other place. 我想我在JQuery或其他地方犯了一些错误。

Please advice. 请指教。

Just have a look in here: http://jqueryui.com/autocomplete/ 请看这里: http//jqueryui.com/autocomplete/

 $("#poojaName").autocomplete({
        source: ["poojaOne","poojaTwo"."poojaThree"],
        selectFirst: true
 })

When you are populating your pooja list with autocomplete.php you should use Ajax to bring the list in and in your PHP file you are better off echoing out an array using json_encode . 当您使用autocomplete.php pooja列表时,您应该使用Ajax将列表放入PHP文件中,最好使用json_encode回显数组。 Have a look in here http://php.net/manual/en/function.json-encode.php to see how to encode a PHP array to JSON. 请查看http://php.net/manual/en/function.json-encode.php ,了解如何将PHP数组编码为JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM