Solved

passing string from PHP to Javascript using JSON

Posted on 2014-10-01
7
435 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
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 52

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 108

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 108

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
can windows 10 wamp send mail()? 3 32
object oriented programming comparison 5 52
Calculating number of days between two dates 7 30
Error in JQuery 5 34
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

948 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

22 Experts available now in Live!

Get 1:1 Help Now