EICT
asked on
passing string from PHP to Javascript using JSON
Hi,
I'm having problems passing a php string variable to javascript using JSON.
I want to check the value of the JSON response in javascript and respond accordingly.
Snippets of my code are as follows:
"close_pdp_process_check" PHP Page
receiving Page
Is the problem with the IF statement because response would be a JSON object? If so how to I return its value.
Thank you
I'm having problems passing a php string variable to javascript using JSON.
I want to check the value of the JSON response in javascript and respond accordingly.
Snippets of my code are as follows:
"close_pdp_process_check" PHP Page
$output = "yes";
echo json_encode($output,JSON_FORCE_OBJECT);
receiving Page
$.ajax({
type: "GET",
url: "close_pdp_process_check.php",
dataType: "json",
success: function(response){
if(response == "yes")
{
alert(" The answer was yes");
}
return false;
}
Is the problem with the IF statement because response would be a JSON object? If so how to I return its value.
Thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Ray for your very helpful article. JSON encoding the string on the PHP page was the problem. Thanks also Leakim971 your suggestion about echoing an array also worked. Thanks for the specific example.
Thanks for the points. It's a great question, ~Ray
echoing an arrayPerhaps just misspoken, but that is an object, not an array in leakim's post. Arrays in Javascript use square brackets = )
JSON syntax is in the right sidebar here:
http://json.org/
http://json.org/
Either way the code below runs fine for me - what problems are you having?
Open in new window