Ted
Ted

Reputation:

jQuery to take url with variables from a link and send it via Ajax

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&boutique_id[]=36&func=Module::Cart->add">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>

Upvotes: 3

Views: 12580

Answers (4)

Chris
Chris

Reputation: 93

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

Upvotes: 1

lod3n
lod3n

Reputation: 2903

Take out that "url="+

Also, url might not be the best variable name.

Upvotes: 0

Anatoliy
Anatoliy

Reputation: 30103

var url = $(this).attr("href");
alert("Now I want to call this page: " + url);
$.get(url, function (resp) {
    alert("Product added to cart");
});

Upvotes: 7

Ryall
Ryall

Reputation: 12281

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

url: url, 

Upvotes: 1

Related Questions