0

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?

1
  • The error message in the popup is just html code returned from the server saying that you have a PHP error in line 8 of cart.php Commented Feb 28, 2013 at 19:32

2 Answers 2

1

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);
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.