Note that there are some explanatory texts on larger screens.

plurals
  1. POPhp, jQuery, and AJAX interaction and data manipulation
    primarykey
    data
    text
    <p>I'm looking for some basic guidance. I understand jQuery. And I understand PHP. I'm having a bit of difficulty wrapping my head around utilizing AJAX.</p> <p>I can use AJAX to pull data from PHP and subsequent database content, but how to I use it in the HTML?</p> <p>What I'm accustomed to:</p> <pre><code>&lt;?php echo '&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;'; $query = mysql_query("SELECT something FROM sometable WHERE field='.$field.'"); while ($row = mysql_fetch_array($query)) { $id = $row['id']; $name = $row['name']; if (str_len($name &gt;= 15)) { $name = substr($name,0,15)."..."; } $icon = ''; switch($id) { case 1: $icon = 'A'; case 2: $icon = 'B'; case 3: $icon = 'C'; } echo '&lt;a href="#'.$id.'"&gt;'.$icon.$name.'&lt;/a&gt;&lt;br /&gt;'; } echo '&lt;/body&gt;&lt;/html&gt;'; ?&gt; </code></pre> <p><strong>How do I use AJAX in a similar fashion? <br />Or should I even try something similar with AJAX?</strong></p> <p>For example, I can do this (inside surrounding correct HTML):</p> <pre><code> $.ajax({ url: 'api.php', data: "", dataType: 'json', success: function(rows) { for (var i in rows) { var row = rows[i]; var id = row[0]; var vname = row[1]; $('#output').html('&lt;a href="#id'+id+'"&gt;'+vname+'&lt;/a&gt;&lt;br /&gt;'); } } }); </code></pre> <p>What I'm having difficulty with is understanding how to implement the switch and other data manipulations as AJAX items/data/objects.</p> <p>The api data call is similar to this: </p> <pre><code>$result = mysql_query("SELECT something FROM sometable WHERE field =$field"); $data = array(); while ($row = mysql_fetch_row($result)) { $data[]=$row; } echo json_encode($data); </code></pre> <p>Is this where I should manipulate the data? If so, is there a specific manner which is best?:</p> <p><strong>Do I simply set up the data manipulations in PHP? If so, how to do pass that data via AJAX properly?</strong> </p> <p>Or should I be using jQuery/javascript to perform all data manipulations?</p> <p>I've searched and 90% of the AJAX tutorials I find are simple in nature and aren't really explaining these sort of things. </p> <p>Edit: General console output of the JSON data looks like this:</p> <pre><code>success: function(data) { $.each(data, function(index, item){ var name; for(name in item){ console.log(name + " = " + item[name]);} }); } </code></pre> <p>and the console:</p> <pre><code>{ 0 = id } { 1 = name } { 2 = 3.7 } { 3 = Y } { 4 = 0 } </code></pre>
    singulars
    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