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 for diafol
$r['zipCode'] = $_GET['zipCode'];
//really you should clean this 

echo json_encode($r);
$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 for diafol

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");
	}
	});

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

Member Avatar for diafol

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.

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

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.

Member Avatar for diafol

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

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.