Note that there are some explanatory texts on larger screens.

plurals
  1. POSending PHP Array to jQuery with $.post() and then looping through those values
    text
    copied!<p>I'm sending an array to jQuery and then looping through those values and displaying them, but I can get it to work right. Can someone point me in the right direction - </p> <p>PHP - the only part that matters on it - <code>echo json_encode($tR);</code></p> <p>jQuery -</p> <pre><code>$(document).on('click', '.chooseStat', function(){ var id = $(this).attr('id'); $.post('chooseStat.php', {id:id}, function(data){ console.log(data); var open = ''; for(var i = 0; i &lt; data.length; i++) { if (i % 2 == 0) { open+="&lt;tr class='alt'&gt;"; open+="&lt;td&gt;"+data[i]+"&lt;/td&gt;"; open+="&lt;td&gt;"+data[i]+"&lt;/td&gt;"; open+="&lt;td&gt;"+data[i]+"&lt;/td&gt;"; open+="&lt;/tr&gt;"; } else { open+="&lt;tr&gt;"; open+="&lt;td&gt;"+data[i]+"&lt;/td&gt;"; open+="&lt;td&gt;"+data[i]+"&lt;/td&gt;"; open+="&lt;td&gt;"+data[i]+"&lt;/td&gt;"; open+="&lt;/tr&gt;"; } } $('#statDisplayBody').html(open); }); }); </code></pre> <p>Heres what the array looks like when it gets sent to the jQuery script - <a href="http://prntscr.com/24wa68" rel="nofollow">http://prntscr.com/24wa68</a></p> <p>Heres what it looks like when I try to display them with the method in the script I have - <a href="http://prntscr.com/24wadk" rel="nofollow">http://prntscr.com/24wadk</a></p> <p>My new jquery -</p> <pre><code>$(document).on('click', '.chooseStat', function(){ var id = $(this).attr('id'); $.post('chooseStat.php', {id:id}, function(data){ console.log(data); var open = '&lt;th&gt;Rank&lt;/th&gt;&lt;th&gt;Username&lt;/th&gt;&lt;th&gt;Stat&lt;/th&gt;'; for(var i = 0; i &lt; data.length; i++) { if (i % 2 == 0) { open+="&lt;tr class='alt'&gt;"; open+="&lt;td&gt;"+data[i]["rank"]+"&lt;/td&gt;"; open+="&lt;td&gt;"+data[i]["username"]+"&lt;/td&gt;"; open+="&lt;td&gt;"+data[i]["score"]+"&lt;/td&gt;"; open+="&lt;/tr&gt;"; } else { open+="&lt;tr&gt;"; open+="&lt;td&gt;"+data[i]["rank"]+"&lt;/td&gt;"; open+="&lt;td&gt;"+data[i]["username"]+"&lt;/td&gt;"; open+="&lt;td&gt;"+data[i]["score"]+"&lt;/td&gt;"; open+="&lt;/tr&gt;"; } } $('#statDisplayBody').html(open); }, "json"); }); </code></pre> <p>And that returns a proper array now, but the way I'm looping through apparently doesn't seem to be working because now this is happening - <a href="http://prntscr.com/24wcuz" rel="nofollow">http://prntscr.com/24wcuz</a> (its only displaying the last row of the array).</p>
 

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