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.

e.g. 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']; ?>" >



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" '; ?>

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.


$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 need to put some kind of loop for that.

For Example : foreach($record => $values){
                   if($values == $_POST['postage']){
                       $selected = "selected='selected' ";
                       $selected = "";

<input name="postage" value="1" <?=$selected?> >


                      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;?> />

I think this may be helpful to can ask me if anything else needed...


The value of selected should be 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:

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

You can also make it shorter:

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

Need Your Help

casting derivedclassptrVector from baseclassptrVec, the best way?

c++ polymorphism casting stl

I am getting base class pointer vector that needed to be passed to derived class pointer vector, please suggest best way to do the same.

Allow only numeric input in a text input, and allow a hyphen as an optional first character

javascript html prototypejs

I need to automatically clean user entry into a text input - to only allow numbers, except for the first character, which could be a number or a hyphen.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.