tags:

views:

671

answers:

4

Hello,

I can add a product to cart via GET with this link:

<div id="add"><a id="add-link" href="http://localhost/catalog/cart?product_id=8&amp;boutique_id[]=36&amp;func=Module::Cart-&gt;add"&gt;Add to Cart</a></div>

I want to use jQuery Ajax to stay on the same page (if JS is enabled). I have crapped out the following, but of course it does not work. Could someone please look what is wrong? Thanks.

<script type="text/javascript">
$(document).ready(function(){

$('a#add-link').bind("click", function(event) {
event.preventDefault();
var url = $(this).attr("href");
    alert("Now I want to call this page: " + url);
$.ajax({
  type: "GET",
  url: "url="+url, 
  success: function() {
    alert("Product added to cart");
  }
});
});

});
</script>
+1  A: 

You don't need "url=", just use

url: url,
Nat Ryall
+5  A: 
var url = $(this).attr("href");
alert("Now I want to call this page: " + url);
$.get(url, function (resp) {
    alert("Product added to cart");
});
Anatoliy
Ooh, that's better.
lod3n
Thanks Anatoliy and everybody who answered! It works now.
Ted
You are welcome )
Anatoliy
A: 

Take out that "url="+

Also, url might not be the best variable name.

lod3n
A: 
$(document).ready(function(){
  $('a#add-link').bind("click", function(event) {
    event.preventDefault();
    $.get($(this).attr('href'), function(data, status) {
      alert("Product added to cart");
    });
});
Chris