How To Show A Drop-down List With A Pre-selected Option
I want to update my form when I click edit button then all info is showing correctly but status value is showing all time same open option. I dont know why it is showing same open
Solution 1:
Use selected
attribute.
<selectname="status"id="status"><optionvalue="open"<?phpif($status=="open") { echo"selected"; } ?> >Open</option><optionvalue="done"<?phpif($status=="done") { echo"selected"; } ?> >Done</option><optionvalue="pending"<?phpif($status=="pending") { echo"selected"; } ?> >Pending</option><optionvalue="working"<?phpif($status=="working") { echo"selected"; } ?> >Working</option></select>
Solution 2:
This is wrong way, correct way is to use like this,
<selectname="status"><?php$options = array("open","done","pending","working");
$selected = "done";
foreach($optionsas$option){
if($selected==$option){
echo'<option value="'.$option.'" selected="selected">'.ucfirst($option).'</option>';
}else{
echo'<option value="'.$option.'">'.ucfirst($option).'</option>';
}
}
?></select>
Solution 3:
If you really want to embed the status in the options tag, you should use the 'selected' attribute for selection of the tag options. Here is your modified code with correct handling:-
<p><labelclass="field"for="username">UserName:</label><inputname="username"type="text"id="username"value="<?phpecho$username;?>"size="50" /></p><p><labelclass="field"for="Status">Status</label><selectname="status"id="status" ><optionvalue="open"<?phpecho$status == 'open' ? 'selected' : ''; ?>>Open</option><optionvalue="done"<?phpecho$status == 'done' ? 'selected' : '' ;?>>Done</option><optionvalue="pending"<?phpecho$status == 'pending' ? 'selected' : '' ; ?>>Pending</option><optionvalue="working"<?phpecho$status == 'working' ? 'selected' : '' ; ?>>Working</option></select></p>
Solution 4:
You need selected tag. Modify your form like this
<selectname="status"id="status"><!-- value removed, there's no use of it here --><optionvalue="open"<?phpif ($status=="open") {echo"selected"}?>>Open</option><optionvalue="done"<?phpif ($status=="done") {echo"selected"}?>>Done</option>
....
</select>
Solution 5:
Use selected attribute for option.
Change your code as
<selectname="status"id="status"><optionvalue="open"<?php selected($status, 'open'); ?>>Open</option><optionvalue="done"<?php selected($status, 'done'); ?>>Done</option><optionvalue="pending"<?php selected($status, 'pending'); ?>>Pending</option><optionvalue="working"<?php selected($status, 'working'); ?>>Working</option></select><?phpfunctionselected($selected_value, $value) {
if( $selected_value === $value ) {
echo'selected=true';
}
}
?>
Post a Comment for "How To Show A Drop-down List With A Pre-selected Option"