3

I am trying to return a json encoded string from PHP script.

$.post("order.php", { product: product_id, coupon: coupon },
function(data) {
    $("#price").html("$" + data.price);
    $("#discount").html("$" + data.discount);
    $("#total").html("$" + data.total);
});

I tried to use alert function in callback to see returning value from PHP:

{"price":249,"discount":"0.00","total":249}

but value of #price and rest elements is "$undefined".

Please help.

0

6 Answers 6

5

It seems you just have to parse the JSON data into an object using parseJSON() :

$.post("order.php", { product: product_id, coupon: coupon },
function(data) {
    data = $.parseJSON( data );
    $("#price").html("$" + data.price);
    $("#discount").html("$" + data.discount);
    $("#total").html("$" + data.total);
});
Sign up to request clarification or add additional context in comments.

Comments

3

I know this is old, but this is for helping all the people who might have this problem

You have to add an extra parameter ('json') to the $.post method call like this:

$.post(url,data_in,function(data_out){ your_code here },'json');

for those who don't know, your PHP must output something like this at the end of all processing:

die(json_encode(array("RESPONSE"=>"OK","MYMESSAGE"=>"THIS IS SUCCESSFULL")));

Also there is an alternative for HTML output instead of JSON:

$.post(url,data_in,function(data_out){ your_code here },'html');

then your responding PHP must output this:

echo "<div id=\"RESPONSE\">OK</div>";   
echo "<div id=\"MYMESSAGE\">THIS IS SUCCESSFULL</div>";
die();

So to respond the original question, the code must be like this:

$.post("order.php", { product: product_id, coupon: coupon }, function(data) {
    $("#price").html("$" + data.price);
    $("#discount").html("$" + data.discount);
    $("#total").html("$" + data.total);
},'json');

Comments

0

you're getting string from your server you should parse it as json

use

$.post("order.php", { product: product_id, coupon: coupon },
function(data) {
    data = jQuery.parseJSON( data );
    $("#price").html("$" + data.price);
    $("#discount").html("$" + data.discount);
    $("#total").html("$" + data.total);
});

Comments

0

you can simply use the script below just remove $array and replace it with what you want to return :

The first two headers prevent the browser from caching the response (a problem with IE and GET requests) and the third sets the correct MIME type for JSON.

let say that this is the content of the order.php file:

begin script

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$array = array("price"=>10,"product_id"=>1);
echo json_encode($array);

end script

and with :

$.post("order.php",function(data){
alert(data.price);
});

we will get 10 and that's it ,simple enough:)

Comments

0

Just add 'json' to the end

$.post("order.php", { product: product_id, coupon: coupon },
function(data) {
    // data.price, ..
}, 'json');

Comments

-1

You should use $.getJSON instead of $.post to get json string.

Don't forget to set your content header to JSON

1 Comment

According to jQuery docu getJSON() uses a GET request. OP wants to use a POST request instead.

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.