Using Php $_post To Remember A Option In A Select Box?
Solution 1:
You almost got it. You need to set the attribute selected="selected"
(the exact form you need technically depends on your HTML doctype, but this is a safe default) on the <option>
element if and only if the value of $postage
equals the value of the element. So:
<selectname="postage"><optionvalue="foo"<?phpif ($_POST['postage'] == "foo") echo'selected="selected" '; ?>
></select>
Note that this violates the DRY principle because you now have two occurrences of the string "foo"
in there, so it's a prime candidate for refactoring. A good approach would be to keep value/text pairs in an array and iterate over it with foreach
to produce the <option>
tags.
Solution 2:
You need to foreach through all the options.
Make an array with all the dropdown options, loop through that, and compare with what is stored in post.
E.G.:
<?php$aDropd = array("apple","orange","banana");
echo"<select>";
foreach($aDropdas$sOption){
$sSel = ($sOption == $_POST['postage'])? "Selected='selected'":"";
echo"<option $sSel>$sOption</option>";
}
echo"</select>";
Solution 3:
no its not working at all..you need to put some kind of loop for that.
For Example : foreach($record => $values){
if($values == $_POST['postage']){
$selected = "selected='selected' ";
}else{
$selected = "";
}
}
<input name="postage" value="1"<?=$selected?> >
EDITED:
if($_POST['postage'] == 1){
$selected1 = "selected='selected' ";
}elseif($_POST['postage'] == 2){
$selected2 = "selected='selected' ";
} and so on..........
<select name="postage">
<option value="1"<?=$selected1;?> />
<option value="2"<?=$selected2;?> />
</select>
I think this may be helpful to you..you can ask me if anything else needed...
Thanks.
Solution 4:
The value of selected should be selected
<selectname="postage"value="1"<?phpecho (($_POST['postage'] == 1)?'selected="selected"':''); ?> >
Solution 5:
Your html syntax is wrong. The correct way to write the html is like this:
<select><optionvalue ="<?phpecho$_POST['postage']; ?>"selected="selected"></option></select>
Post a Comment for "Using Php $_post To Remember A Option In A Select Box?"