I need an example of how to code a jQuery autocomplete to populate product_id while showing the product_name calling an ajax page "remote.php"
<input name="product_name" id="product_name" type="text" value="" /> <input name="product_id" id="product_id" type="hidden" value="" /> remote.php: $partial = addslashes($_POST['partial_search']); $myDataRows = array(); $result = mysql_query ("SELECT product_id, product_name FROM products WHERE product_name like "%$partial%"); while ($row = mysql_fetch_row($result)) { array_push($myDataRows, $row); } $ret = json_encode ($myDataRows); echo $ret;
I'm not sure how to code the jQuery autocomplete and if I need to change remote.php
thanks
ADDED LATER:
I worked out another solution:
<script type="text/javascript"> function nqi_search (type, id_name, text_name) { $( "#"+text_name ).autocomplete({ source: "remote.php?&t="+type, minLength: 1, select: function( event, ui ) { $( "#"+id_name ).val(ui.item.id ); } }); } </script> <script type="text/javascript"> jQuery(document).ready(function() { nqi_search ("product_search", "product_id", "product_name"); // also you can have many on one page with: nqi_search ("vendor_search", "vendor_id", "vendor_name"); }); </script>
There's one problem. it doesn't seem to work if the nqi_search function is put into a .js file. I have no idea why?