We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,089 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

2
Contributors
9
Replies
18 Hours
Discussion Span
1 Year Ago
Last Updated
10
Views
ferrari77
Newbie Poster
15 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
$r['zipCode'] = $_GET['zipCode'];
//really you should clean this 

echo json_encode($r);
diafol
Keep Smiling
Moderator
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,512
Skill Endorsements: 57
$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
Skill Endorsements: 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
Keep Smiling
Moderator
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,512
Skill Endorsements: 57

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

ferrari77
Newbie Poster
15 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
Keep Smiling
Moderator
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,512
Skill Endorsements: 57

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 = {
ferrari77
Newbie Poster
15 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
Skill Endorsements: 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
Skill Endorsements: 0

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

diafol
Keep Smiling
Moderator
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,512
Skill Endorsements: 57

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0891 seconds using 2.69MB