Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing POST with Query JSON and iterating MySQL results
    text
    copied!<p>I'm not sure where i'm going wrong here. </p> <p>I'm trying to have a table of data change when a user clicks on a different item quantity.</p> <p>I might be using the $.each() method incorrectly, because i'm not seeing any results on the page and i'm a complete jQuery noob. I appreciate any help, thanks</p> <p>test-table.html</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;test&lt;/title&gt; &lt;script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function swapContent(count){ $(".price-sidebar").html("Put animation here").show(); $.ajax({ type: "POST", dataType: 'json' url: "myphpscript.php", data: {countVar: count}, success: function(data){ $('#content').empty(); $.each(data, function(){ $("#content").append("&lt;tr&gt;"); $("#content").append("&lt;td class='store-row'&gt;&lt;h5 property='seller'&gt;"+this[MerchantName]+"&lt;/h5&gt;&lt;/td&gt;"); $("#content").append("&lt;td class='price-row'&gt;&lt;h5 property='price'&gt;$"+this[Price]+"&lt;/h5&gt;&lt;/td&gt;"); $("#content").append("&lt;td&gt;&lt;a property='url' target='_blank' href='"+this[PageURL]+"' class='btn btn-danger'&gt;GET IT&lt;/a&gt;&lt;/td&gt;"); $("#content").append("&lt;/tr&gt;"); }) } }); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;a href="#" onClick="return false" onmousedown="javascript:swapContent('18');"&gt;18&lt;/a&gt; &lt;a href="#" onClick="return false" onmousedown="javascript:swapContent('48');"&gt;48&lt;/a&gt; &lt;a href="#" onClick="return false" onmousedown="javascript:swapContent('96');"&gt;96&lt;/a&gt; &lt;section class="price-sidebar span8" &gt; &lt;div property="offers" typeof="Offer"&gt; &lt;h2&gt;Price Comparison&lt;/h2&gt; &lt;/br&gt; &lt;table class="price-data"&gt; &lt;tr&gt; &lt;th class='store-row'&gt;&lt;h4&gt;Store&lt;/h4&gt;&lt;/th&gt; &lt;th class='price-row'&gt;&lt;h4&gt;Price&lt;/h4&gt;&lt;/th&gt; &lt;/tr&gt; &lt;div id="content"&gt; &lt;/div&gt; &lt;/table&gt;&lt;!--end price-data--&gt; &lt;/div&gt;&lt;!--end offers--&gt; &lt;/section&gt;&lt;!--end price sidebar--&gt; &lt;/body&gt; </code></pre> <p>myphpscript.php</p> <pre><code> &lt;?php $countVar = $_POST['countVar']; $data = PriceCompare($countVar); echo json_encode($data); function PriceCompare($countVar){ $DBH = new PDO('mysql:host=localhost;dbname=--','---','---'); $STH = $DBH-&gt;query('SELECT ProductID, MerchantName, Price, PageURL FROM merchants WHERE ProductID=677 AND Count=' . $countVar . ' ORDER BY Price'); $result = $STH-&gt;fetchAll(); return $result; } ?&gt; </code></pre>
 

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