Reputation: 124
I am trying to write a live update shopping cart. I've tried to run one function inside the other, but can only get it to work on reload. What is wrong in my approach here?
EDIT: I've now edited from checkKart to checkKart();
$(function checkKart() {
$.ajax({
type: "GET",
url: "checkCart.php",
success: function(html) {
$("#handlevogn").html(html);
}
});
});
function addToCart(product_name, design_name, price, qty) {
$.ajax({
type: "GET",
url: 'test.php',
data: {qty: qty, product_name: product_name, design_name: design_name, price: price},
success: function(data) {
alert("Varen er i handlekurven");
checkKart();
}
});
return "Varen er i handlekurven";
}
Upvotes: 0
Views: 88
Reputation: 339816
Your error console should be telling you that checkKart
is an undefined function, the reason being that as written:
$(function checkKart() {
$.ajax({
...
});
});
what you've created is a "named function expression", in which the name is only visible within that function, and the name is not exported to the enclosing scope.
If you define the function normally without the $(...)
wrapper, everything should work.
Upvotes: 1
Reputation: 941
function checkKart() {
$.ajax({
type: "GET",
url: "checkCart.php",
success: function(html) {
$("#handlevogn").html(html);
}
});
};
function addToCart(product_name, design_name, price, qty) {
$.ajax({
type: "GET",
url: 'test.php',
data: {qty: qty, product_name: product_name, design_name: design_name, price: price},
success: function(data) {
alert("Varen er i handlekurven");
checkKart();
}
});
return "Varen er i handlekurven";
}
That should work.
not sure why you had your first function wrapped inside of $(). if you were trying to write a jQuery plugin the proper syntax is this:
(function( $ ) {
$.fn.myPlugin = function() {
// Do your awesome plugin stuff here
};
})( jQuery );
which you can read more about here http://docs.jquery.com/Plugins/Authoring
Upvotes: 1