Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery json loop through data - just cant figure this out
    primarykey
    data
    text
    <p>I have a button on a pge thats fetches json data from a php page,</p> <p>the data seems to arrive ok but i have gone through hundreds of examples and i just cant seem to reference the returned data, here is my script:</p> <p>EDITED SCRIPT from previous comments</p> <pre><code> $('#geo_batch').click(function (){ var ajax_load = "&lt;label&gt;&lt;img src='/images/icons/loadinfo.gif' alt='saving location...' /&gt; Loading data...&lt;/label&gt;"; $("#batch_detail").html(ajax_load); $('#batch_buttons').hide(); var form = $("form"); //Grab the form element from the DOM //alert(form.serialize()); var mydata = form.serialize(); $.ajax({ type: "POST", url: 'geo_getupdate_list.php', data: mydata, datatype: 'json', success: function(dat) { alert('dat:'+ typeof dat ) //RETURNS STRING //alert(dat.location[0].id_mdt); //RETURNS UNDEFINED // Cache the batch_detail element var $detail = $("#batch_detail").html('&lt;label&gt;Locations have been retrieved:&lt;br&gt;' + dat + '&lt;label&gt;'); $('#batch_buttons').show(); // Instead of several .append() calls on the same element, create a // single string, and do one. var appendString = ''; for(var key in dat) { alert(key); return false; }; /*for(i=0; i &lt; count; i++){ appendString += 'display address: ' + data.location[i].displayaddr_mdt + 'flag: ' + data.location[i].flag_mdt; }*/ $detail.append(appendString); }, error: function(dat) { //Triggered if an error communicating with server //alert('fail'); $("#batch_detail").html('&lt;label&gt;There was an error: '+dat+'&lt;label&gt;'); $('#batch_buttons').show(); } }); return false; //Ignore the default behavior of the button click }); </code></pre> <p>the json that is returned is:</p> <pre><code>{"location":[{"id_mdt":"5","idetp_mdt":"1","name_mdt":null,"geoaddr_mdt":null,"displayaddr_mdt":"31a Arundel Gardens London W11 2LW","telephone_mdt":null,"email_mdt":null,"website_mdt":"http:\/\/","lat_mdt":"51.513561","lng_mdt":"-0.206519","active_mdt":"1","flag_mdt":"1","id_etp":"1","name_etp":"Stockist","icon_etp":null},{"id_mdt":"1","idetp_mdt":"1","name_mdt":null,"geoaddr_mdt":null,"displayaddr_mdt":"via todde 29 Ales Sardegna 09091","telephone_mdt":null,"email_mdt":null,"website_mdt":"http:\/\/","lat_mdt":"39.7670964","lng_mdt":"8.813689","active_mdt":"1","flag_mdt":"1","id_etp":"1","name_etp":"Stockist","icon_etp":null},{"id_mdt":"4","idetp_mdt":"1","name_mdt":null,"geoaddr_mdt":null,"displayaddr_mdt":"31a Arundel Gardens London W11 2LW","telephone_mdt":null,"email_mdt":null,"website_mdt":"http:\/\/","lat_mdt":"51.513561","lng_mdt":"-0.206519","active_mdt":"1","flag_mdt":"1","id_etp":"1","name_etp":"Stockist","icon_etp":null},{"id_mdt":"3","idetp_mdt":"1","name_mdt":null,"geoaddr_mdt":null,"displayaddr_mdt":"31a Arundel Gardens London W11 2LW","telephone_mdt":null,"email_mdt":null,"website_mdt":"http:\/\/","lat_mdt":"51.513561","lng_mdt":"-0.206519","active_mdt":"1","flag_mdt":"1","id_etp":"1","name_etp":"Stockist","icon_etp":null},{"id_mdt":"6","idetp_mdt":"1","name_mdt":null,"geoaddr_mdt":null,"displayaddr_mdt":"31a Arundel Gardens London W11 2LW","telephone_mdt":null,"email_mdt":null,"website_mdt":null,"lat_mdt":"51.513561","lng_mdt":"-0.206519","active_mdt":"1","flag_mdt":"1","id_etp":"1","name_etp":"Stockist","icon_etp":null},{"id_mdt":"7","idetp_mdt":"1","name_mdt":"Test","geoaddr_mdt":null,"displayaddr_mdt":"31a Arundel Gardens London W11 2LW","telephone_mdt":null,"email_mdt":null,"website_mdt":null,"lat_mdt":"51.513561","lng_mdt":"-0.206519","active_mdt":"1","flag_mdt":"1","id_etp":"1","name_etp":"Stockist","icon_etp":null}]} </code></pre> <p>how do i access the data in the array?</p> <p>i have tried so many examples on here and other places and i cant get any to work. I think it may be to do with the returned json object it has [ symbols in it which i think is wrong?</p> <p>the php i have to generate the json is as follows:</p> <pre><code>//have defined a recordset called $rs_locations $rows = array(); while($r = mysql_fetch_assoc($rs_locations)) { $rows[] = $r; } $jsondata = json_encode($rows); // trying to strip out the [ ] brackets but doesn't work str_replace ("[", "", $jsondata); str_replace ("]", "", $jsondata); echo($jsondata); </code></pre> <p>any ideas anyone, i am so stuck, thanks</p>
    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.
 

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