Solved

passing string from PHP to Javascript using JSON

Posted on 2014-10-01
7
444 Views
Last Modified: 2014-10-01
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
$output =  "yes";

echo json_encode($output,JSON_FORCE_OBJECT);

Open in new window


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;
    }

Open in new window


Is the problem with the IF statement because response would be a JSON object? If so how to I return its value.

Thank you
0
Comment
Question by:EICT
7 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 40354224
This does not make sense.  There is no added value to JSON-encoding a string variable.  It would only make sense if you were passing an array, object or other complex data structure.  You can just pass the string, as shown in this article.  Easy!
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 total points
ID: 40354252
$output =  '{"answer":"yes"}';
echo $output;

Open in new window


$.ajax({
    type: "GET",
    url: "close_pdp_process_check.php",
    dataType: "json",
    success: function(response){
    
    if(response.answer == "yes")
    {
      alert(" The answer was yes");
    }
    return false;
    }

Open in new window

0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 40354253
What is the problem - code runs correctly - see below. Agree with Ray - pointless doing a JSON request for a single string return but I suspect this is just test code and you are probably going to expand to include more complex data.
Either way the code below runs fine for me - what problems are you having?
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
	$.ajax({
		type: "GET",
		url: "close_pdp_process_check.php",
		dataType: "json",
		success: function(response){
			if(response == "yes") {
				console.log(" The answer was yes");
			}
			return false;
		}                                  
	});
});
</script>
</head>
<body>
Testing
</body>
</html>

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Closing Comment

by:EICT
ID: 40354298
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.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40354300
Thanks for the points.  It's a great question, ~Ray
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40354314
echoing an array
Perhaps just misspoken, but that is an object, not an array in leakim's post. Arrays in Javascript use square brackets  = )
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40354319
JSON syntax is in the right sidebar here:
http://json.org/
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question