Page 1 of 1

[SOLVED] desperatly need help

Posted: Thu Aug 12, 2004 4:37 pm
by Think Pink
hello. I'm trying for many days now to make a dropdown that has an onchange event.
I have the following drpdown :

Code: Select all

<?php
<form name="country" action="<? $PHP_SELF; ?>" method="post">
<select name="chooseCountry" onChange="submitpage()">
<option value='0'>Choose country</option>
<?
$querydrop = "select * from countryes";
$result = mysql_query($querydrop);

while($row = mysql_fetch_array($result)) 
{ 
if($row['id']==$chooseCountry) 
{ 
print "<option value="".$row['id']." selected">".$row['country_name']."</option>"; 
} 
else 
{ 
print "<option value="".$row['id']."">".$row['country_name']."</option>"; 
} 
} 
?>
</select>
</form>
?>
OK. When I select a country, the form is submited, the page reloaded and and the selected option is the first in the list.

I want that after the form is submited and the page reloaded, the selected country to be the one I choosed before.

Pls help
Thx a lot

Posted: Thu Aug 12, 2004 4:58 pm
by feyd
since you likely don't have register_globals on, switch $chooseCountry to $_POST['chooseCountry']

Posted: Thu Aug 12, 2004 5:16 pm
by Think Pink
sorry.
in my code is $_POST['chooseCountry'].
I just typed it fast here so I made a mistake. Good point though.
But still it doesn't work.

Posted: Thu Aug 12, 2004 5:19 pm
by feyd
is it also in your real code that the value of the selected one would print out with "selected" in it?

Posted: Thu Aug 12, 2004 5:24 pm
by markl999
You have your 'selected' bit in the wrong place, if you do 'view source' after submitting you'll see something like: value="2 selected"
Try:

Code: Select all

if($row['id']==$_POST['chooseCountry'])
{
echo '<option value="'.$row['id'].'" selected">'.$row['country_name'].'</option>';
}
else
{
echo '<option value="'.$row['id'].'">'.$row['country_name'].'</option>';
}

Posted: Thu Aug 12, 2004 5:27 pm
by Think Pink

Code: Select all

<?php
while($row = mysql_fetch_array($result)) 
{ 
if($row['id']==$chooseCountry) 
{ 
print "<option value="".$row['id']." selected">".$row['country_name']."</option>"; 
} 
else 
{ 
print "<option value="".$row['id']."">".$row['country_name']."</option>"; 
} 
}
?>
I modified it with

Code: Select all

<?php
while($row = mysql_fetch_array($result))
{
print "<option value='".$row['id']."'";
if($_POST['chooseCountry']==$row['id']) echo " selected";
print ">".$row['country_name']."</option>";
}
?>
aparently now is working. So simple.
Thankyou