Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

passing string from PHP to Javascript using JSON

Posted on 2014-10-01
7
Medium Priority
?
465 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 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 1000 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 59

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
TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

 

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 111

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 111

Expert Comment

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

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

704 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