Reputation: 5393
Hi I am just learning to work with jquery and ajax.And an tryin gto berform a basic jquery call and retrieve an ok.But it seems I get back nothing.
This is my html:
<a href="#" class="addToCart" id="'.$idProduct.'" name="cart">Add to Cart</a>
This is my jquery code:
$('.addToCart').on('click', function(){
var itemId = $(this).attr("id");
$.ajax({
url: 'cart.php',
type: 'POST',
data: itemId,
dataType:'html',
success: function(result){
alert(result + " ceva ");
},
error : function(data){
alert(data);
}
});
});
And this is my php code:
echo $_POST['cart'];
When I try to run this in the success alert I get back this:
How can make this ajax call to work properly?
Upvotes: 0
Views: 1856
Reputation: 3909
Looks to me like you have an error in your PHP code. The returned HTML has some text that says "Notice: Undefined index" etc.
The AJAX Call succeeds - so you're seeing the alert message.
Upvotes: 0
Reputation: 97672
You have to send your post data in key/value pairs, try
$.ajax({
url: 'cart.php',
type: 'POST',
data: {cart:itemId},//key -> cart, value -> itemId
dataType:'html',
success: function(result){
alert(result + " ceva ");
},
error : function(data){
alert(data);
}
});
Upvotes: 1