Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery Autocomplete id and item
    primarykey
    data
    text
    <p>I have a working query autocomplete code that completes the full_name when letters are typing. What I am trying to figure out is how to get the user_id for that goes with the full_name. I have JSON that comes back like so:</p> <pre><code>[{"full_name":"Matt","user_id":"2"},{"full_name":"Jack","user_id":"9"},{"full_name":"Ace","user_id":"10"},{"full_name":"tempaccount","user_id":"11"},{"full_name":"Garrett","user_id":"26"},{"full_name":"Joe","user_id":"29"},{"full_name":"Raptors","user_id":"32"}] </code></pre> <p>Below is my jQuery code. I am using PHPfox framework.</p> <pre><code>$(function(){ //attach autocomplete $("#to").autocomplete({ //define callback to format results source: function(req, add){ //pass request to server //$.getJSON("friends.php?callback=?", req, function(data) { $.ajaxCall('phpfoxsamplee.auto', 'startsWith='+req.term) .done(function( data ) { //create array for response objects var suggestions = []; var data = $.parseJSON(data); //process response $.each(data, function(i, val){ //suggestions.push(val.full_name,val.user_id); (This works and shows both the full name and id in the dropdown. I want the name to be visible and the ID to goto a hidden input field) suggestions.push({ id: val.user_id, name: val.full_name }); }); //pass array to callback add(suggestions); }); }, //define select handler select: function(e, ui) { //create formatted friend alert(ui.item.full_name); //Trying to view the full_name (doesn't work) alert(ui.item.id); // trying to view the id (doesn't work) var friend = ui.item.full_name, (doesn't work) //var friend = ui.item.value, (This works if I do not try to push labels with values) span = $("&lt;span&gt;").text(friend), a = $("&lt;a&gt;").addClass("remove").attr({ href: "javascript:", title: "Remove " + friend }).text("x").appendTo(span); //add friend to friend div span.insertBefore("#to"); $("#to").attr("disabled",true); $("#to").attr('name','test').attr('value', 'yes'); $("#to").hide(); }, //define select handler change: function() { //prevent 'to' field being updated and correct position $("#to").val("").css("top", 2); } }); //add click handler to friends div $("#friends").click(function(){ //focus 'to' field $("#to").focus(); }); //add live handler for clicks on remove links $(".remove", document.getElementById("friends")).live("click", function(){ //remove current friend $(this).parent().remove(); $("#to").removeAttr("disabled"); $("#to").show(); //correct 'to' field position if($("#friends span").length === 0) { $("#to").css("top", 0); } }); }); </code></pre> <p>HTML</p> <pre><code>&lt;div id=friends class=ui-help-clearfix&gt; &lt;input id='to' type=text name='player[" . $num . "][name]'&gt;&lt;/input&gt; &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload