Use Zip to Pre-Populate City/State Form with jQuery AJAX
Posted
by
Paul
on Stack Overflow
See other posts from Stack Overflow
or by Paul
Published on 2012-10-08T15:36:16Z
Indexed on
2012/10/08
15:36 UTC
Read the original article
Hit count: 231
I'm running into a problem that I can solve fine by just submitting a form and calling a db to retrieve/echo the information, but AJAX seems to be a bit different for doing this (and is what I need). Earlier in a form process I ask for the zip code like so:
<input type="text" maxlength="5" size="5" id="zip" />
Then I have a button to continue, but this button just runs a javascript function that shows the rest of the form. When the rest of the form shows, I want to pre-populate the City input with their city, and pre-populate the State dropdown with their state. I figured I would have to find a way to set city/state to variables, and echo the variables into the form. But I can't figure out how to get/set those variables with AJAX as opposed to a form submit. Here's how I did it without ajax:
$zip = mysql_real_escape_string($_POST['zip']);
$q = " SELECT city FROM citystatezip WHERE zip = $zip";
$r = mysql_query($q);
$row = mysql_fetch_assoc($r);
$city = $row['city'];
Can anybody help me out with using AJAX to set these variables? Thanks!
© Stack Overflow or respective owner