jQuery UI autoComplete - pass it a value from another field
I'm attaching jQuery UI autocomplete to a contact field which works, but now I need to also pass a company_id (dynamcially) to restrict the search not only to the search characters but also to a company that the user has previously chosen. IE if the user enter 'jo' all contacts that contain 'jo' that are within the company_id passed.
Incidentally, remote.p开发者_如何学Gohp passes back id, and value so I can populate both the contact_name and contact_id. That part works.
I just can't figure out how to pass it the company_id field.
$( "#contact_name ).autocomplete({ source: 'remote.php?t=contactSearch', minLength: minlen, select: function( event, ui ) { $( "#contact_id" ).val(ui.item.id ); } });
thanks!
After much research and trail and error, this works for me.
So what this does:
The user has chosen a company before this code (not shown),
The autocomple below is attached the contact_name, and the company_id is also passed.
remote.php returns an array of both id and value of the contacts that meet the partial search criteria and belong to the given company. When the user clicks on a value, the contact name is placed in the field of the autocomplete(there is no special code for that, autocomplete does that automatically) and id is placed in the contact_id field.
$( "#contact_name" ).autocomplete({ source: function(request, response) { $.getJSON ('remote.php', { term: request.term, company_id:$('#company_id').val() }, response ); }, select: function( event, ui ) { $( "#contact_id" ).val(ui.item.id ); } }); A look at a simplified version of remote.php is: $myDataRows = array (); $search = addslashes($_REQUEST['term']); $company_id = addslashes($_REQUEST['company_id']); $sql = "SELECT c.contact_id as `id`, contact_name as`value` FROM contacts c WHERE c.company_id = '$company_id' and c.contact_name LIKE '%$search%' "; $result = mysql_query ($sql); while ($row = mysql_fetch_assoc($result)) { array_push($myDataRows, $row); } $ret = json_encode ($myDataRows); echo $ret;
精彩评论