Note that there are some explanatory texts on larger screens.

plurals
  1. POProperly assemble multidimensional ajax JSON into global array
    primarykey
    data
    text
    <p>I have two jQuery AJAX request being made when the use searches for customer information. I can get the customer name via .autocomplete and I can get JSON data from addressSearch.php, but when I cannot get the values i need from the second JSON set.</p> <p>The user should simply select the address from the drop down and i should fill in the form.</p> <p>When I <code>console.log(address)</code> I do not get the correct multidimensional array.</p> <p>How do I select the correct sub-array to send data to the form fields?</p> <p><strong>HTML</strong></p> <pre><code>&lt;label&gt;Customer Search:&lt;/label&gt;&lt;input id="cSearch" type="text" /&gt;&lt;br /&gt; &lt;label&gt;Name:&lt;/label&gt;&lt;input id="name" /&gt;&lt;br /&gt; &lt;select id="addresses"&gt; &lt;option&gt;Please Select Address&lt;/option&gt; &lt;/select&gt; &lt;label&gt;Street:&lt;/label&gt;&lt;input id="street" type="text" /&gt;&lt;br /&gt; &lt;label&gt;City:&lt;/label&gt;&lt;input id="city" type="text" /&gt;&lt;br /&gt; &lt;label&gt;State:&lt;/label&gt;&lt;input id="state" type="text" /&gt;&lt;br /&gt; &lt;label&gt;Zip:&lt;/label&gt;&lt;input id="zip" type="text" /&gt;&lt;br /&gt; </code></pre> <p><strong>JavaScript</strong></p> <pre><code>$(document).ready(function() { $("input#customerSearch").autocomplete({ source:'inc/customerSearch.php', select: function(event, ui) { $('#name').val(ui.item.name); getAddresses(ui.item.addressid); }, minLength:2 }); }); var address = []; function getAddresses(id){ $.getJSON( 'inc/addressSearch.php', { id: id }, function(data) { $('#addresses').children().remove().end().append('&lt;option&gt;Please Select Address&lt;/option&gt;'); $.each(data, function(x, val) { $('#addresses').append('&lt;option value="' + val['addressid'] + '"&gt;'+ val['street1'] +'&lt;/option&gt;'); address[x] = []; $.each(val, function(key, value) { address[x][key] = value; }); }); } ); $('select#addresses').change(function() { var j = $(this).val(); //fill in fields when select }); } </code></pre> <p><strong>addressSearch.php</strong></p> <pre><code>$id = explode(',', $id);//explode address ids $data = array();//data array foreach($id as $id) {//evaluate each address $query = "SELECT * FROM `address` WHERE address_id = '". $id ."'";//query $result = mysql_query($query);//run query if (mysql_num_rows($result)) {//results exist while($row = mysql_fetch_assoc($result)) {//loop $data[$id] = array(//build array 'addressid' =&gt; $row['address_id'], 'street1' =&gt; $row['address_street1'], 'street2' =&gt; $row['address_street2'], 'city' =&gt; $row['address_city'], 'region' =&gt; $row['address_region'], 'country' =&gt; $row['address_country'], 'code' =&gt; $row['address_code'] ); } } } print json_encode($data);// jQuery wants JSON data </code></pre> <p><strong>JSON</strong></p> <pre><code>{ "101":{ "addressid":"101", "street1":"123 ABC St", "street2":"", "city":"Somewhere", "region":"AB", "country":"USA", "code":"12345" }, "102":{ "addressid":"102", "street1":"123 10th St", "street2":"Apt 101", "city":"Nowhere", "region":"AB", "country":"USA", "code":"12345" } } </code></pre>
    singulars
    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