简体   繁体   中英

Using PHP $_POST to remember a option in a select box?

I have a form which POSTs to itselft so the user can do things that you would find in a Shopping Cart.

eg Increase quantity, select postage type.

My problem is for my form Select element called "postage" whenever the form reloads itself , it forgets what was selected.

All my other fields remember their values using this:

<input type="text" name="postcode" value="<?php echo $_POST['postcode']; ?> " />

How do I use the $_POST value to automatically select the option in the select field that was done by the user?

I tried this:

<select name="postage" selected="<?php echo $_POST['postage']; ?>" >

and this

<select name="postage" value="<?php echo $_POST['postage']; ?>" >

Thanks

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:

<select name="postage">
<option value="foo" 
    <?php if ($_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.

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.

EG:

<?php
$aDropd = array("apple","orange","banana");
echo "<select>";
foreach($aDropd as $sOption){
  $sSel = ($sOption == $_POST['postage'])? "Selected='selected'":"";
  echo "<option   $sSel>$sOption</option>";
}
echo "</select>";

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' ";
                          }else if($_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.

You can also make it shorter:

<select>
<option value ="<?=$_POST['postage']; ?>" selected="selected"></option>
</select>

应该选择selected的值

<select name="postage" value="1" <?php echo (($_POST['postage'] == 1)?'selected="selected"':''); ?> >

Your html syntax is wrong. The correct way to write the html is like this:

<select>
<option value ="<?php echo $_POST['postage']; ?>" selected="selected"></option>
</select>
<select name="foo">
<option value="1" <?php echo strcmp($_POST['foo'],"1")==0?"selected=\"selected\"":"" ?>>option1</option>
<option value="2" <?php echo strcmp($_POST['foo'],"2")==0?"selected=\"selected\"":"" ?>>option2</option>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM