List Box - Selected item display

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
the_jub
Forum Newbie
Posts: 1
Joined: Fri Nov 14, 2003 1:40 pm

List Box - Selected item display

Post by the_jub »

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";
}
?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Hmm, try this:

Code: Select all

...............
print "<option value="$country_id"";
if($row[1]==$url_id) { 
   print "selected"; 
} 
print ">$row[0]</option>\n"; 
...............
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

You could see if this code snippet is suitable for you: PHP/HTML: Print <select><options></select>.

Cheers,
Scorphus.
Post Reply