954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

$.Ajax and JSON

Problemo:

My PHP Script (it's just a snippet) is not picking up my JavaScript code. It doesn't even see the zip code field. It says it doesn't exist:

<?php
    $zipCode = $_GET[zipCode];
	
	//echo "data:".$zipCode;
	$json = "{data:.$zipCode}";
	
	echo $json;
	
	//echo json_encode(array("data"=>$zipCode));
	
	//print ("Zip Code:".$zipCode);
?>


I'm passing it in the following code block:

function getCityAndState(data, location)
			{
				var jsonString = {"zipCode":  data};
				alert("JSON String:" + jsonString.zipCode); 
				if (location === "living")
				{	
					$("#livingCityField").val("");
					$("#livingStateField").val("");
					alert("Inside getCityAndState: " + location);
					$.ajax({
     						type: "GET",
     						url: "getCityAndState.php",
     						async: true,
     						dataType: "json",
							data: {zipCode:  data},
 							success: function(data){
    							alert("SUCCESS:" + data.zipCode);
 							},
							complete: function(data)
							{
								alert("No bueno");
							}
							});


At this point I'm just trying to pass it a zip code and retrieve that zip code both in JSON format. Help! I've been working on this for six hours!
By the way this doesn't use a form but fires on event handling. It fires when there are five characters typed into a JQuery modal window with form objects embedded through a .
Ferrari

ferrari77
Newbie Poster
15 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 
$r['zipCode'] = $_GET['zipCode'];
//really you should clean this 

echo json_encode($r);
diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 
$r['zipCode'] = $_GET['zipCode'];
//really you should clean this 

echo json_encode($r);

I implemented the changes you suggested. It didn't change anything.

ferrari77
Newbie Poster
15 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

OK, just to test it:
PHP

echo "hello";


JS

$.ajax({
     	type: "GET",
     	url: "getCityAndState.php",
     	async: true,
     	data: {zipCode:  data},
 	success: function(data){
    	alert("SUCCESS:" + data);
 	},
	complete: function(data)
	{
	alert("No bueno");
	}
	});
diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

It didn't work. Why wouldn't it?

ferrari77
Newbie Poster
15 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

It fire an alert() on this:

alert("JSON String:" + jsonString.zipCode);


So you've obviously got the jQuery library referenced properly and you are actually calling the function.
Try clearing the cache in your browser, and if you're using a templating engine, delete any tmp files. Try again.

If still no joy, have a look at the php file reference - is it valid? Does it need a prefix path like "/includes/..." ?

If still no joy...

function getCityAndState(data, location){
  
  var request = $.ajax({
  url: "getCityAndState.php",
  type: "GET",
  data: {zipCode : data},
  dataType: "html",
  success: function(transport){
    	alert("SUCCESS:" + transport);
  }
});
}


And just echo some simple text in the php file. Just to see that it works for you.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

So 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 = {

ferrari77
Newbie Poster
15 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

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);
    						});
					
					
				}
ferrari77
Newbie Poster
15 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

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

ferrari77
Newbie Poster
15 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

OK, this is js - you may find it easier to post to the js forum.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: