Nistor Alexandru
Nistor Alexandru

Reputation: 5393

jQUery ajax call return an html error

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:

enter image description here

How can make this ajax call to work properly?

Upvotes: 0

Views: 1856

Answers (2)

nwahmaet
nwahmaet

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

Musa
Musa

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

Related Questions