Solved

passing string from PHP to Javascript using JSON

Posted on 2014-10-01
7
430 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 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
$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 51

Expert Comment

by:Julian Hansen
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Closing Comment

by:EICT
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points.  It's a great question, ~Ray
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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 108

Expert Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Summernote and form validation 10 38
Safari SVG Image Problem 1 19
Editing login page in zencart. 2 13
ajax form sumbit 3 22
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now