I have problem where when the user selects the country from the list box it then selects a list of airports from the database and displays them in the next list box, this works fine however the first list box no longer shows the original country selection but instead shows "Pick a country"
I am completely new to coding so please explain you answers in layman's terms.
Please see my attempt at code below
Can anyone help
Thanks
<?php
// Register the user in the database.
require_once('mysql_connect.php'); //Connect to DB
//$dbh=mysql_connect ("localhost", "morphy_internet", "internet") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("morphy_whichresort");
?>
<select name="Country" onChange="location.href='test2.php?&url_id=' + this.options[selectedIndex].value;">
<option value="">Pick A Country</option>
<?php
// get country list
$query = "SELECT DISTINCT c_name,c_id FROM country";
$result = mysql_db_query ("morphy_whichresort",$query);
while ($row = mysql_fetch_array($result))
{
$country_value = urlencode($row[0]);
$country_id = urlencode($row[1]);
print "<option value=\"$country_id\"";if(strcmp($row[0],$url_country) == 0) {
print "selected";
}
print ">$row[0]</option>\n";
}
?>
</select>
<select name="city">
<? /* get all distinct cities */
$query = "SELECT DISTINCT a_city FROM airport WHERE c_id = '$url_id'
";
$result = mysql_db_query("morphy_whichresort",$query);
while ($row = mysql_fetch_array($result)) {
print "<option value=\"$row[0]\">$row[0]</option>\n";
}
?>
List Box - Selected item display
Moderator: General Moderators
Hmm, try this:
Code: Select all
...............
print "<option value="$country_id"";
if($row[1]==$url_id) {
print "selected";
}
print ">$row[0]</option>\n";
...............- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
You could see if this code snippet is suitable for you: PHP/HTML: Print <select><options></select>.
Cheers,
Scorphus.
Cheers,
Scorphus.