Created using both JavaScript and PHP.
How it is supposed to work is that when a country is selected, an array for cities appears that coincide with the selected country
My tables within the database is similar to the following:
tblCountry
1 UK
2 US
tblCity
1 London 1
2 New York 2
3 Miami 2
4 Brighton 1
However, when I choose UK as a country for some reason it selects US (so php.?country=2 in stead of country=1), though it does show London and Brighton as the city array.
Of course, what I would like it to do is show up as UK when country=1 and the same is US' case.
Any help would be appreciated. Thanks in advance.
Here's the code:
Code: Select all
$query2 = "SELECT countryID, country FROM `tblUrlcountry` ORDER BY country ASC";
$result2 = mysql_query($query2);
$cnt= $_GET['country'];
if(isset($_GET['country']) and strlen($_GET['country']) > 0){
$query = "SELECT tblUrlcity.cityID, tblUrlcity.city FROM tblUrlcity, tblCountrycity WHERE tblCountrycity.cityID = tblUrlcity.cityID AND tblCountrycity.countryID = '$cnt' ORDER BY city ASC";
$result = mysql_query($query);
}else{
$query = "SELECT cityID, city FROM `tblUrlcity` ORDER BY city ASC";
$result = mysql_query($query);
}
<SCRIPT language=JavaScript>
function reload(form)
{
// Setting the variable with the value of selected country's ID
var val=query.country.options[query.country.options.selectedIndex].value;
// Sending the country id in the query string to retrieve the city list
self.location='<?php echo $_SERVER['PHP_SELF']; ?>?country=' + val ;
}
</script>Code: Select all
echo "<table width='470' border='0' cellspacing='0' cellpadding='0'><tr><td width='135' align='left'>";
echo "<select onChange=\"reload(this.form)\" name='country' class=formsection />
<option value=''>Select Country</option>";
while($row = mysql_fetch_array($result2, MYSQL_NUM)) {
if($result2[0] == $_GET['country']) {
echo '<option value="',$row[0],'">' ,$row[1], '</option>';
}else{
echo '<option selected value="',$row[0],'">' ,$row[1], '</option>';
}
}
echo "</select></td><td width='109'>";
echo "<select name='city' class=formsection /><option value=''>Select City</option>";
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo '<option value="',$row[0],'">' ,$row[1], '</option>';
}
echo "</select></td>";
echo "<td align='left'><INPUT TYPE=IMAGE SRC='images/go.gif' NAME='go'></td></tr></table>";