3

I am trying to return multiple values from a php process.

Here is the jQuery function

$.ajax({
    url: 'shopping_cart_add.php',
    data: 'item_id='+subscription_id+'&item_name='+subscription_name+'&item_price='+subscription_price,
    type: 'POST',
    dataType: 'json',
    success: function(response, statusText) {
                var qty = response.item_quantity;
                $("#shopping-cart-quantity").html(qty);
    }
});

The above seems to work except I can't retrieve the specific field values from the returned JSON.

When I try this...

var qty = response.item_quantity;
$("#shopping-cart-quantity").html(qty);

Nothing happens.

If I change...

$("#shopping-cart-quantity").html(qty);

to

$("#shopping-cart-quantity").html(response);

I get the following...

{ 'account_id': '1', 'item_id' : 'cce3d2a017f6f1870ce8480a32506bed', 'item_name' : 'CE', 'item_quantity' : '1', 'item_price' : '1' }
3
  • Does response have a responseJSON property that has the object you're looking for? Commented Sep 28, 2015 at 19:26
  • @ScottKaye Since he's using dataType: 'json', response is already parsed, it's not the XHR. Commented Sep 28, 2015 at 19:28
  • 3
    Your JSON is incorrect. Strings must be contained in double quotes, not single quotes. You have a bug in your PHP script, it's not calling json_encode() to encode the response. Commented Sep 28, 2015 at 19:29

1 Answer 1

1

Please make sure that you are using json_encode() for returning result array

/*** PHP ***/
echo json_encode($resultArr); exit ;

And in AJAX try with eval() to access response text value .

/*** AJAX ***/
var qty = eval(response.item_quantity);
Sign up to request clarification or add additional context in comments.

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.