NamshanNet
NamshanNet

Reputation: 426

How to set selected value in drop list in HTML?

How to set selected value in drop list in HTML in Edit Form?

The selected value came from MySql

This code is not work !! >> selected=""

<select name="TutorGender" id="TutorGender" selected="<?php echo $Gender ?>">
    <option value="Male">Male</option>
    <option value="Female">Female</option>
</select>

Upvotes: 2

Views: 28586

Answers (10)

Wil DeCarde
Wil DeCarde

Reputation: 1

In a case where I needed to use a select as part of a user update form that was populated via a database I found the array option works best. The line by line option can get very unwieldy very quickly. This way you simply need to add an option to the $optionDept array should any new options be required. In this example "$row['Dept']" is from the larger overarching results from the database request.

$optionDept= array('sales','warehouse','inspection','production','accounts','admin','inactive');

$optionCount= count($optionDept);

echo"<select name=\"UserDept\">";
for($x = 0; $x < $optionCount; $x++) {
echo"<option value=".$optionDept[$x];
if($optionDept[$x]==$row['Dept']){echo " selected";}
echo">".$optionDept[$x]."</option>";}
echo"</select</P>

Upvotes: 0

thatNewGuy
thatNewGuy

Reputation: 3

just another way of doing it. I prefer to use shorthand if statements for code such as this.

<select name="TutorGender" id=\"TutorGender\">
  <option value="Male" <?php echo ($Gender == "Male" ? "selected" : ""); ?>>Male</option>
  <option value="Female" <?php echo ($Gender == "Female" ? "selected" : ""); ?>>Female</option>

here is a link to using shorthand "if" statements.

Upvotes: 0

user3318789
user3318789

Reputation: 1

This worked for me: I send througt the GET method of the submit the value i want to restore, and then in the php, AT THE END OF THE PAGE (if not, default values will be set if the code is before the php) i set those values.

echo "select_language.selectedIndex = ".$_GET['iL'].";";

Upvotes: -1

Swapnil T
Swapnil T

Reputation: 547

try this

if you want to keep male as selected

<select name="TutorGender" id="TutorGender" >
<option value="Male" selected="selected">Male</option>
<option value="Female">Female</option>
</select>

Upvotes: -1

rlatief
rlatief

Reputation: 723

<select name="TutorGender" id="TutorGender">

<?php
$genders = array( 'Male', 'Female' );
foreach( $genders as $value )
{
    $selected = ( $Gender == $value )? ' selected="selected"': '';
    echo '<option value="' . $value . '"' . $selected . '>' . $value . '</option>';
}
?>

</select>

Upvotes: 0

Charles
Charles

Reputation: 756

W3Schools does a good job of explaining this here http://www.w3schools.com/tags/att_option_selected.asp

Your code might look something like this.

 <select name="TutorGender" id="TutorGender">
      <option value="Male" <?php if($Gender == "Male") echo "selected"; ?>>Male</option>
      <option value="Female" <?php if($Gender == "Female") echo "selected"; ?>>Female</option>
 </select>

Though my PHP syntax may be a little off, but you should get the idea.

Upvotes: 13

Gareth Cornish
Gareth Cornish

Reputation: 4356

<select name="TutorGender" id="TutorGender">
    <option value="Male" <?php if($Gender=='Male') echo 'selected' ?>>Male</option>
    <option value="Female" <?php if($Gender=='Female') echo 'selected' ?>>Female</option>
</select>

Upvotes: 2

Wezy
Wezy

Reputation: 667

<option <?php if($Gender == 'Male'){ echo(' selected '); } ?> value="Male">Male</option>
<option <?php if($Gender == 'Female'){ echo(' selected '); } ?> value="Female">Female</option>

Upvotes: 0

Quentin
Quentin

Reputation: 944150

The selected attribute appears on the option(s) that should be selected, not the select element (if it went on the select element itself, then (since multiple options can be selected at the same time) it would have to take a list of ids and you would need to add an id to each option).

<option value="foo" selected>Foo</option>

Generally you would have an array of values (or of value/human text pairs) that you would loop over. (i.e. you wouldn't hard code your options)

You would generate one option each time you went around the loop, and test to see if you should include a selected attribute each time.

Upvotes: 0

Zevi Sternlicht
Zevi Sternlicht

Reputation: 5399

Selected goes on the option tag, not on the select tag.

<select name="TutorGender" id="TutorGender" >
<option value="Male" selected="selected">Male</option>
<option value="Female">Female</option>
</select>

Upvotes: 2

Related Questions