Page 1 of 1

drop down menu connection problem.... :(

Posted: Wed Mar 24, 2004 11:20 am
by andycruickshank

Code: Select all

<tr>
    <td>
	<form name=trylistsforum action=<?php echo $_SERVER['PHP_SELF']; ?> method=post>
	<?php
$host="localhost";
$user="root";
$password="";

$connection = mysql_connect($host,$user,$password)
	or die("couldn't connect to server.");
$database = "project";
$db = mysql_select_db($database,$connection)
	or die ("couldn't select database.");


$sql = "SELECT DISTINCT continent FROM continent ORDER BY continent";
$result = mysql_query($sql) or die(mysql_error()) ;
echo "<select name=continent onchange='document.trylistsforum.submit();'>";
if (!isset($continent)) { echo "<option value=null selected>Choose a Region</option>";}
while(list($continent) = mysql_fetch_row($result))
{
echo "<option value=$continent";
if ($continent == $continent) { echo " selected"; }
echo ">$continent</option>";
}
echo "</select>";
echo "<select name=country onchange='document.trylistsforum.submit();'>";
if (!isset($country)) { echo "<option value=null selected>Choose Region First</option>"; }
else
{
$query="select DISTINCT country FROM countries where contID='$continent' ORDER BY country";
$result = mysql_db_query($database,$query,$connection);
echo "<option value=null selected>Choose a Model</option>";
while(list($model_id,$name) = mysql_fetch_row($result))
{
echo "<option value=$country";
if ($country == $country) { echo " selected"; }
echo ">$name</option>";
}
}

echo "</select>";
?>
</td>
hi there, i'm using the above code i found on a forum for creating drop down boxes, where the value selected from the first one stays, and the second is populated using the value selected, but the drop down box does not stay, and the second one does not populate. I want to use this code over again for further lists, but getting this two working would be a good start! :P

Any help? I'll eventually use a submit button to submit all the values, but that shouldn't be too bad...

Thank you

Posted: Wed Mar 24, 2004 11:28 am
by magicrobotmonkey
You can't do it this way you are going to have to use javascript. Once the page loads, the php is done. So you can't populate the second box based on the first using php. you could use php to set up a javascript function which will do it, but php cannot do it directly. The user does not intereact directly with php.

also, unless il missing something here,

Code: Select all

<?php
    if ($continent == $continent) 
?>
Is going to be true every time. There are some severe flaws in the logic here. Also, when you select from the databse based on $continent, all you are going to do is select all the countries for whichever continent is last in the list in the first box, because that it was the variable is set to. The webpage will not change the value of $continent or any php var.