0

This code is working (as is) on a live site. I am attempting to run it on localhost via XAMPP but addToCart.php is not being called.

Wondering if there's a better way to write this so it is more robust and will work on localhost config as well. I am not very familiar with JQuery or PHP, as may be obvious from the code.

<a id="atcAnchorTag'.$thisProduct['id'].'" 
href="#" 
onclick="
jQuery.ajax(\'./addToCart.php?id='.$thisProduct['id'].'\');
jQuery(atcAnchorTag'.$thisProduct['id'].').hide();
jQuery(rfcAnchorTag'.$thisProduct['id'].').show();
jQuery(mt'.$thisProduct['id'].').hide();
jQuery(grn'.$thisProduct['id'].').show();
scItemsCountJS++ ;
return false ; "
style="display:'.$atcDisplayType.'">add to cart</a>
4
  • 1
    Ehhh, why is this all inline? Put this into a JavaScript file and store all of the info you need stored in an element with data- attributes. That way, you can attach your onclick stuff via an event handler. Commented Jun 14, 2012 at 5:04
  • Sorry to ask but I need some help with this. How would I re-write the above code to do what you suggest? I'm not yet sufficiently advanced to follow your directions without an example. Commented Jun 14, 2012 at 5:26
  • 1
    I don't think there's much you can do to "fix" this code, as it would require you to restructure quite a lot of things. How do you know that the AJAX call isn't running? Commented Jun 14, 2012 at 5:28
  • If I type localhost/addToCart.php?id=2 in the browser address bar, then a product is added to the cart. Just clicking the "add to cart" link makes pretty jQuery events happen (show/hide) but nothing is added to the cart. Commented Jun 14, 2012 at 5:31

1 Answer 1

1

Actually, Blender was correct and I would credit him with the answer if there was a way to do so. Rewriting the code as he suggested turned out not to be very difficult but it took a long time to figure it out myself.

Here is what worked. HTML:

<a id="atcAnchorTag'.$thisProduct['id'].'" 
    href="#" 
    style="display:'.$atcDisplayType.'">
    add to cart
</a>

JQuery:

<script type="text/javascript">
    $("#atcAnchorTag'.$thisProduct['id'].'").click(function() { 
        //alert("Hello");
        jQuery(atcAnchorTag'.$thisProduct['id'].').hide();
        jQuery(rfcAnchorTag'.$thisProduct['id'].').show();
        jQuery(mt'.$thisProduct['id'].').hide();
        jQuery(grn'.$thisProduct['id'].').show();
        scItemsCountJS++ ;
        var prodid = '.$thisProduct['id'].';
        $.post("addToCartJQ.php", {id: prodid}, function() {
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.