MySQL Lookup problem
I'm developing a form that asks the user to input a zipcode and I'd like to store the associated City and State in a hidden field in the form when the user clicks "Submit". This ought to be straightforward so I must be doing something wrong that is blindingly obvious and I'm way out of my depth. This is partly working but I can't get the MySQL Lookup to work - the page doesn't load when I enable the Lookup code.
I also need to allow for non-US zipcodes that aren't in the lookup table, in which case I'll just copy the zip field contents into the City/State field - I know it could be an invalid US zip code but I can live with that. I don't know how to get the lookup to return the zipcode if it doesn't find a match.
I'd really appreciate some direction on this. Below is what I have so far. Thanks,
I have a lookup table containing two columns, zipcode and cityState, I have a form containing among other things two fields, one called "zip" and the other hidden field called "location" - which should store the City and State obtained from the lookup table.
So, below is the code I have with additional comments:
<script type="text/javascript">
function getCitySt(){
// copy the zipcode to the location field in case it is a non-US zip - this code works
document.form1.location.value = document.form1.zip.value;
// Now do the lookup - the page fails to load when this code is enabled
document.form1.location.value = "
<?php
// Make a MySQL Connection
$query = "SELECT zipcodes.citystate FROM zipcodes WHERE document.form1.zip.value = zipcodes.zipcode";
// I think I have to change the following line so if the lookup fails it returns the zip code as the result of the lookup
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result))
{
echo $row['citystate'];
}
?>
"
}
</script>
