Populating City/State from ZIP with jQuery/PHP

Hey all, I’m new to jQuery and hoping to get some direction on my current project as everything I have found so far has been too far from what I am trying to do for me to learn from and adapt to my needs.

The little project I am working on is setting up a web form such that when the user enters their zip code (US only), a call is made to a php function which returns the appropriate City and State data. The one spot I am having difficulty with is in retrieving this data from the php file. I have read and reread the pages in the jQuery manual about .get, .post, .ajax, etc. and I am somewhat at a loss as to how to go about picking up the data that the php script generates.

Any help is greatly appreciated.

Thanks in advance.

Jay

Sorry I hadn’t posted this yet. Been rather busy. Seeing as someone is actually asking for it, though, I figure I’ll put it up for that person and maybe some critique if anyone with more chops than me would care to look it over.

I’m only going to post the jQuery/Javascript code as the rest pretty much is just simple HTML and PHP MySQL queries that I expect people reading already know how to handle. If I’m making bad assumptions there, feel free to let me know and I will post more as needed. That said, the jQuery code used is as follows:


$(document).ready(function() {

	$("#fields_zip").keyup(function() {
		var input_zip = $('#fields_zip').val();
		var textlength = input_zip.length;
		if(textlength > 4)
		{

			$.get('citystate.php', { method: 'city', zip: input_zip }, function(output_city)
			{
				$("#fields_city").val(output_city);
			});

			$.get('citystate.php', { method: 'state', zip: input_zip }, function(output_state)
			{
				$("select#fields_state").val(output_state);
			});

		}
	});
});

HTH

Jay

I managed to get it to work and will post my solution here later for others who might be curious and also for critique as I’m sure it could be done more elegantly.

Any update you can post? I’m trying to figure it out too…