1,105,633 Community Members

$.Ajax and JSON

Member Avatar
ferrari77
Newbie Poster
15 posts since Dec 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 <div></div>.
Ferrari

Member Avatar
diafol
Where are my eyes?
12,991 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,849 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 
$r['zipCode'] = $_GET['zipCode'];
//really you should clean this 

echo json_encode($r);
Member Avatar
ferrari77
Newbie Poster
15 posts since Dec 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
$r['zipCode'] = $_GET['zipCode'];
//really you should clean this 

echo json_encode($r);

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

Member Avatar
diafol
Where are my eyes?
12,991 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,849 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
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");
	}
	});
Member Avatar
ferrari77
Newbie Poster
15 posts since Dec 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
diafol
Where are my eyes?
12,991 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,849 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
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.

Member Avatar
ferrari77
Newbie Poster
15 posts since Dec 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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:

        function getCityAndState(data, location)
        {
            var jsonString = {
Member Avatar
ferrari77
Newbie Poster
15 posts since Dec 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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);
    						});
					
					
				}
Member Avatar
ferrari77
Newbie Poster
15 posts since Dec 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
diafol
Where are my eyes?
12,991 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,849 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: