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
Think Pink
Forum Contributor
Posts: 106 Joined: Mon Aug 02, 2004 3:29 pm
Post
by Think Pink » Thu Aug 12, 2004 4:37 pm
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Aug 12, 2004 4:58 pm
since you likely don't have register_globals on, switch $chooseCountry to $_POST['chooseCountry']
Think Pink
Forum Contributor
Posts: 106 Joined: Mon Aug 02, 2004 3:29 pm
Post
by Think Pink » Thu Aug 12, 2004 5:16 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Aug 12, 2004 5:19 pm
is it also in your real code that the value of the selected one would print out with "selected" in it?
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Aug 12, 2004 5:24 pm
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>';
}
Think Pink
Forum Contributor
Posts: 106 Joined: Mon Aug 02, 2004 3:29 pm
Post
by Think Pink » Thu Aug 12, 2004 5:27 pm
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