Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery if/else statements
    text
    copied!<p>i'm trying to write some if/else conditions within a jQuery ajax method. I don't know jQuery well, so i'm probably just making a stupid small syntax error. But here it is: </p> <pre><code> function swapContent(count,product) { $.ajax( { type: "POST", dataType: 'json', url: "includes/price-update.php", data: {countVar: count, ProductID: product}, success: function(data) { console.log(data); $('.cleardata').remove(); $.each(data, function () { $(".price-data").append( $("&lt;tr class='cleardata'/&gt;") if (data.InStock == "In Stock") { $('.in-stock-row').text ('Yes'); } else if (data.InStock == "Unavaiable"){ $('.in-stock-row').text ('No'); } else{ $('.in-stock-row').text ('-'); } .append("&lt;td class='store-row'&gt;&lt;h5 property='seller'&gt;" + this.MerchantName + "&lt;/h5&gt;&lt;/td&gt;") .append("&lt;td class='price-row'&gt;&lt;h5 property='price'&gt;$" + this.Price + "&lt;/h5&gt;&lt;/td&gt;") .append("&lt;td class='in-stock-row'&gt;&lt;h5&gt;" + this.InStock + "&lt;/h5&gt;&lt;/td&gt;") .append("&lt;td class='merch-row'&gt;&lt;a property='url' target='_blank' href='" + this.PageURL + "' class='merch-but'&gt;GET IT&lt;/a&gt;&lt;/td&gt;") ); }) } }); } &lt;/script&gt; </code></pre> <p>Everything works great aside from the if/else statments. I'm not sure if <code>return</code> is the correct way of echoing out data. I am a PHP guy, so the jQuery is still all new. Thanks for any help. </p> <p>EDIT: I just changed the code according to some suggestions and this is what I have, and nothing in my table is showing up now. </p> <p>Second EDIT: Attaching an image of JSON data for debugging. <img src="https://i.stack.imgur.com/aqe1Z.png" alt="JSON Data from ajax post method. Not sure why there is a 3 and InStock with the same properties."></p> <p>Third EDIT: Posting PHP data </p> <pre><code>&lt;?php $countVar = $_POST['countVar']; $ProductID = $_POST['ProductID']; $data = PriceCompare($countVar, $ProductID); echo json_encode($data); function PriceCompare($countVar, $ProductID){ $DBH = new PDO('mysql:host=localhost;dbname=---','---','---'); $STH = $DBH-&gt;query('SELECT MerchantName, Price, PageURL, InStock FROM merchants WHERE ProductID=' . $ProductID .' AND Count=' . $countVar . ' ORDER BY Price'); $result = $STH-&gt;fetchAll(); return $result; } ?&gt; </code></pre> <p>EDIT Four: Fixed the JSON data from getting two sets of the data by changing the php <code>fetchall</code> method to <code>fetchall(PDO::FETCH_ASSOC)</code> Results of fixed JSON data below. Still not getting the correct results in the In Stock table field though. <img src="https://i.stack.imgur.com/G703G.png" alt="Fixed JSON Data"></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