I've simplified it quite a bit but it's still not working.

PHP:

<?php
	
	$a = array('data' => 'Hello');
	echo json_encode($a);
	

?>

Here's the JQuery Code:

function getCityAndState(data, location)
			{
				var jsonString = {"zipCode":  data};
				var outR = outputResults	
				alert("JSON String:" + jsonString.zipCode); 
				if (location === "living")
				{	
					$("#livingCityField").val("");
					$("#livingStateField").val("");
					alert("Inside getCityAndState: " + location);
					//$.get("testfile.php", 
					//	{zipCode:  data}, 
					//	outR,
					//	'text'
				
						
					//	);
						
					$.getJSON("testfile.php",function(d) {
    						alert("JSON Data: " + d.data);
    						});
					
					
				}

The really relevant part of this code is the $.getJSON part.

Recommended Answers

All 10 Replies

try:

<?php
header('Content-Type: application/json');
$a = array('data' => 'Hello');
echo json_encode($a);
exit;
?>

Tried it. Didn't help but didn't hurt either.

What troubleshooting steps have you taken? Do you know for a fact that the getCityAndState() function is called? If yes, does it make it INTO the if clause?
If yes, is the browser (as a whole) refreshing after the function is called?

If the problem persists, try installing Firebug for Firefox. Load your page. Enabled/Launch Firebug (by clicking on the "grayed-out" bug). Then click on the "Net" tab. Once your function is called/executed, you should see the out going JSON ajax request and its result. You need to verify that the request is in fact going to the correct url and the browser successfully contacts the server.

What troubleshooting steps have you taken? Do you know for a fact that the getCityAndState() function is called? If yes, does it make it INTO the if clause?
If yes, is the browser (as a whole) refreshing after the function is called?

If the problem persists, try installing Firebug for Firefox. Load your page. Enabled/Launch Firebug (by clicking on the "grayed-out" bug). Then click on the "Net" tab. Once your function is called/executed, you should see the out going JSON ajax request and its result. You need to verify that the request is in fact going to the correct url and the browser successfully contacts the server.

Thanks Hielo,

1. It makes it into getCityAndState().

2. It makes it into the IF clause.

3. The browser is not refreshed after the function is called but should it have to be since it's just sending an alert?

I'll install Firebug and see what happens.

Did you try the code on your system? If so, did it work?

3. The browser is not refreshed after the function is called but should it have to be since it's just sending an alert?

No, it shouldn't, but it is possible - depending on how you call that function. My question was meant to figure out what else might be on that page of yours since you haven't disclosed much. In your case, you would want the browser to NOT refresh after the function executes. Based on your responses, it seems like you are OK on this regard, so we'll just have to see what Firebug "reports".

Just finished looking at firebug. It's reporting that the Object "d" is not defined therefore it's leading me to believe that it's not getting anything back from the PHP script. When I used the $.get alone and didn't send it a string that told it what type of data I wanted back, i.e. "json", "text", etc. it sent back a XML object. So I know at some level it works.

If you clicked on the "Net" tab of Firebug, you would be able to see the URL the request was sent to. Click on the url. You should see a "Response" tab. This should reveal exactly what the server sends back.

Hielo,

<?php
	header('Content-type: text/plain')
	//$a = array('data' => 'Hello');
	echo "Hello";
	exit;
?>

It's sending back the entire file. I simplified it a bit just to try it out.

What could that mean?

It's sending back the entire file

If I understood you correctly, it is sending back the entire PHP code (instead of what just "Hello"). If that is the case, it typically means that the server is not configured to execute php files. You need to search the internet for articles on how to install PHP for your particular server (IIS, Apache, etc)

If that is the case, it typically means that the server is not configured to execute php files.

Or the file extension is wrong. Typically only .php files are sent to the php engine, but potentially any other extension. For Apache, this is established in httpd.conf.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.