0

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

2
Contributors
5
Replies
7
Views
7 Years
Discussion Span
Last Post by daryll1
0

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>';
		}
0

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.

0

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
}?>

Edited by daryll1: n/a

0

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

0

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.

Attachments
<?php 
$database="test"; 
mysql_connect ("localhost", "root", "******"); 
@mysql_select_db($database) or die( "Unable to select database"); ?>

<?php echo $_REQUEST['hometeam']; ?>

<form action="test.php">
<select name="hometeam">
<OPTION VALUE=0>Please choose the home team</option>

<?php 
$result = mysql_query("SELECT * FROM test_1");
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>

<input type="submit" value="gogogo">
</form>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.