简体   繁体   中英

Using auto complete textbox get table record from mysql database and shows in text fields

I need to access the table record from the database when they press enter in the autocomplete textbox (at the time of select value), and it shows data in text element.

I have tried this code but cannot get the whole record:

  html code:
  <input class="form-control" name="sel_product_name" id="sel_product_name" > 
  <input class="form-control" name="sel_product_id" id="sel_product_id">

 javascript code:
   <script type="text/javascript">
            $(document).ready(function(){
                $("#sel_product_name").autocomplete({
                    source:'autocomplete.php',
                    minLength:1
                });

            });
    </script>

 autocomplete.php 
    <?php
     mysql_connect("localhost","root","");
     mysql_select_db("es_restaurant");
     $term=$_GET["term"];
     $query=mysql_query("SELECT fld_product_id,fld_product_name 
              FROM tbl_product_master_header 
             where fld_product_name  like '%".$term."%' ");
     $json=array();
     while($product_name=mysql_fetch_array($query))
       {
        $json[]=array(
          'value'=> $product_name["fld_product_name"]

          //I want to add one more column value to display fld_product_id in    
           //another text box 
                 );
        }
     echo json_encode($json);
    ?>

Try hanging the code to this:

$json = array();
while($product_name=mysql_fetch_array($query)) {
    $json[]= $product_name["fld_product_name"];
}
echo json_encode($json);

I think because you are adding another layer to the array it is not working.

http://jqueryui.com/autocomplete/ shows using an array without keys. http://www.smarttutorials.net/jquery-autocomplete-search-using-php-mysql-and-ajax/ also confirms this.

As to your comment try this:

 while($product_name=mysql_fetch_array($query)) {
    $json[]= array('id' => $product_name["fld_product_id"], 
                   'value' => $product_name["fld_product_name"]);
 }

http://www.simonbattersby.com/blog/jquery-ui-autocomplete-with-a-remote-database-and-php/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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