after my form has been submitted I want it to remember the option that has been chosen from the drop down list. I now you use SELECTED but not sure how to get it into my code.

this is how my dropdown list is built:

while ($row=mysql_fetch_array($result)) {
        $name=$row["name"];
	$options.="<OPTION VALUE=\"$name">".$name.'</option>';
		}

the html code looks like this:

<select name="hometeam">
<OPTION VALUE=0>Please choose the home team
<?php echo $options ?>
</select>

thanks for any help

Recommended Answers

All 5 Replies

try something like
may have to play around with the quotation marks a bit

while ($row=mysql_fetch_array($result)) {
        $name=$row["name"];
	$options.="<OPTION VALUE=\"$name" <?php if (isset($_REQUEST['name']) && $_REQUEST['name'] == "Mr") {echo 'selected="selected"';} ?>>".$name.'</option>';
		}

try something like
may have to play around with the quotation marks a bit

while ($row=mysql_fetch_array($result)) {
        $name=$row["name"];
	$options.="<OPTION VALUE=\"$name" <?php if (isset($_REQUEST['name']) && $_REQUEST['name'] == "Mr") {echo 'selected="selected"';} ?>>".$name.'</option>';
		}

that didnt work i just get a syntax error.

can you explain to me the above code so I can fully understand it.

Ok try this

It needs to go where you currently have the <?php echo $options ?>

<?php 
while ($data=mysql_fetch_array($result)) {
$name=$data["name"];
print "<OPTION VALUE=".$name; 
// Print the first part of the option
if (isset($_REQUEST['name']) && $_REQUEST['name'] == $name) {echo 'selected="selected"';} 
//insert the if statement to select the right dropdown
print ">".$name."</option>"; 
// print the second part of the option
}?>

Ok try this

It needs to go where you currently have the <?php echo $options ?>

<?php 
while ($data=mysql_fetch_array($result)) {
$name=$data["name"];
print "<OPTION VALUE=".$name; 
// Print the first part of the option
if (isset($_REQUEST['name']) && $_REQUEST['name'] == $name) {echo 'selected="selected"';} 
//insert the if statement to select the right dropdown
print ">".$name."</option>"; 
// print the second part of the option
}?>

thanks for your help but the above code doesnt work. the dropdown box is empty

Hey, sorry for not replying sooner.
Sorted a solution for you, if you don't have one already, see below.

<select name="hometeam">
<OPTION VALUE=0>Please choose the home team</option>

<?php 
$result = mysql_query("SELECT * FROM WHATEVER_YOUR_TABLE IS");
while ($data=mysql_fetch_array($result)) {
print "<OPTION VALUE=".$data["name"]." name=".$data["name"]; 
// Print the first part of the option
if ($_REQUEST['hometeam'] == $data["name"]) {echo ' selected ';} 
//insert the if statement to select the right dropdown
print ">".$data["name"]."</option>"; 
// print the second part of the option
}?>
</select>

I've tested this and it works fine, if you want to try it out I've attached my testing file.

Have fun.
Daryll.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.