SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

Retaining drop down list box value of a form after posting

Learn Details on retaining all post back values of a form

From a drop down list box or combo box visitor selects one option and submits the form. If the form validation fails then we have to show again the same form asking for changes in input data and submit again. In this process we have to keep our drop down box selected to the option previously selected by the user. For example in a travel information form, visitor select the week day of travel from a drop down list box having all days of the week and submits the form, then due to any reason the form validation fails and we ask the visitor to again fill the form, we must keep the previously selected day of the week of the visitor as selected only. So if the visitor has selected Monday then the form has to show day Monday as selected day of the week.

There are different ways to do this and one method where the data (or the option) are populated from table is already discussed in double drop down list box tutorial. There all options are matched with an if condition and accordingly the selected attribute is added. Note that there are many ways to achieve this and we will try to explore one way here.

We will use the switch function here to assign the selected attribute to one option. We are using header redirection to post back the date entered by the user to the form. Here is the demo of the drop down list box.

Here is the code of the form with decode script.

$t1=$_GET['t1'];
switch ($t1)
{
case "Mon":
$Mon="selected";
break;
case "Tue":
$Tue="selected";
break;
case "Wed":
$Wed="selected";
break;
case "Thu":
$Thu="selected";
break;
case "Fri":
$Fri="selected";
break;
case "Sat":
$Sat="selected";
break;
case "Sun":
$Sun="selected";
break;
}

echo "
<form method=post action=pb-dropck.php>
<select name=t1>
<option value=Mon $Mon>Mon</option>
<option value=Tue $Tue>Tue</option>
<option value=Wed $Wed>Wed</option>
<option value=Thu $Thu>Thu</option>
<option value=Fri $Fri>Fri</option>
<option value=Sat $Sat>Sat</option>
<option value=Sun $Sun>Sun</option>
</select>
<input type=submit value=Submit>
";


Here is the code for pb-dropck.php file

$t1=$_POST[t1];
header ("Location: pb-drop.php?t1=$t1");


Here is the demo of the script. Select one radio button and submit.



Discuss this tutorial at forum


Further readings
All form components validation with post back and data locking
Checkbox
How to retain form data if validation fails?
How to retain data of a text box after submitting
Retaining status of a check box
Updating checkbox value from & to a table record.
Retaining selected data of a period button of a form
Retaining selected data of a drop down list box of a form
Validating Date: Checking if date exist
Email address validation in a form
PHP Form Validation
is_numeric to check numeric numbers
ctype_alnum to check alphanumeric characters data
ctype_alpha to check alphabetic characters data
How to take care of form & query string variables if Register global is OFF?














 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.