ajax - jQuery if/else statements -
i'm trying write if/else conditions within jquery ajax method. don't know jquery well, i'm making stupid small syntax error. here is:
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( $("<tr class='cleardata'/>") 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("<td class='store-row'><h5 property='seller'>" + this.merchantname + "</h5></td>") .append("<td class='price-row'><h5 property='price'>$" + this.price + "</h5></td>") .append("<td class='in-stock-row'><h5>" + this.instock + "</h5></td>") .append("<td class='merch-row'><a property='url' target='_blank' href='" + this.pageurl + "' class='merch-but'>get it</a></td>") ); }) } }); } </script>
everything works great aside if/else statments. i'm not sure if return
correct way of echoing out data. php guy, jquery still new. help.
edit: changed code according suggestions , have, , nothing in table showing now.
second edit: attaching image of json data debugging.
third edit: posting php data
<?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->query('select merchantname, price, pageurl, instock merchants productid=' . $productid .' , count=' . $countvar . ' order price'); $result = $sth->fetchall(); return $result; } ?>
edit four: fixed json data getting 2 sets of data changing php fetchall
method fetchall(pdo::fetch_assoc)
results of fixed json data below. still not getting correct results in in stock table field though.
i have added mock json, there typo in spelling of unavailable
in first one. each
section of ajax success function correct. can see working on http://jsfiddle.net/mhwdp/
hope helps. r.
jsondata = [{ "instock": "in stock", "merchantname": "coffee less", "pageurl": "http://www.google.com", "price": "14.99" }, { "instock": "unavailable", "merchantname": "drugstore", "pageurl": "http://www.google.com", "price": "15.99" }, { "instock": "no", "merchantname": "drugstore2", "pageurl": "http://www.google.com", "price": "29.99" }]; $.each(jsondata, function (index, dataitem) { stockstatus = ''; if (dataitem.instock == "in stock") { stockstatus = "yes"; } else if (dataitem.instock == "unavailable") { stockstatus = "no"; } else { stockstatus = "-"; } temprow = document.createelement('tr'); $(temprow).append("<td class='store-row'><h5 property='seller'>" + dataitem.merchantname + "</h5></td>") .append("<td class='price-row'><h5 property='price'>$" + dataitem.price + "</h5></td>") .append("<td class='in-stock-row'><h5>" + stockstatus + "</h5></td>") .append("<td class='merch-row'><a property='url' target='_blank' href='" + dataitem.pageurl + "' class='merch-but'>get it</a></td>") .append("</tr>"); $(".price-data").append(temprow); });
Comments
Post a Comment