Note that there are some explanatory texts on larger screens.

plurals
  1. POShowing and hiding buttons per ajax request in jquery
    text
    copied!<p>I've coded up a sort of inventory managing system and I'm adding a shipping cart so to speak to it. I'm trying to make the interface easier to use and navigate through jquery. The 'cart' is stored via sessions in php. I have a page that outputs all the inventory and I am adding buttons that allow the user to add or remove each specific item from the 'cart', but only one button should be shown based on cart status (i.e. if the item is in cart, show the remove button).</p> <p>Ive got a mess of jquery code as I'm trying all sorts of approaches</p> <p>heres some php:</p> <pre><code> if(isset($_SESSION['cart'][$row['bbn']])) { echo "&lt;a href=\"#\" class=\"active removefromcart\"&gt;REMOVE FROM CART&lt;/a&gt;\n"; echo "&lt;a href=\"#\" class=\"addtocart\"&gt;ADD TO CART&lt;/a&gt;\n"; } else { echo "&lt;a href=\"#\" class=\"active addtocart\"&gt;ADD TO CART&lt;/a&gt;\n"; echo "&lt;a href=\"#\" class=\"removefromcart\"&gt;REMOVE FROM CART&lt;/a&gt;\n"; } </code></pre> <p>here's some jquery:</p> <pre><code>$(".addtocart").each(function (i) { if($(this).hasClass('active')){ $(this).siblings('.removefromcart').hide(); } }); $(".removefromcart").each(function (i) { if($(this).hasClass('active')){ $(this).siblings('.addtocart').hide(); } }); // View_inventory add button $(".addtocart").click(function(){ var $bbn = $(this).parent().attr("id"); var $object = this; $.ajax({ url: "queue.php?action=add", data: { bbn: $bbn }, type: 'GET', success: function(){ $($object).hide(); $($object).siblings('.removefromcart').show('highlight'); } }); }); $(".removefromcart").click(function(){ var $bbn = $(this).parent().attr("id"); var $object = this; $.ajax({ url: "queue.php?action=remove", data: { bbn: $bbn }, type: 'GET', success: function(){ $($object).hide(); $($object).siblings('.addtocart').show('highlight'); } }); }); </code></pre> <p>Any suggestions as to how I should make this simpler? Ive got it working now.</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