SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP Form Form post back

Retaining drop down list box value of a form after posting

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.

Demo of retaining selection of drop down

Here is the code of the form with decode script.

switch ($t1)
case "Mon":
case "Tue":
case "Wed":
case "Thu":
case "Fri":
case "Sat":
case "Sun":

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>
<input type=submit value=Submit>

Here is the code for pb-dropck.php file

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

Mulitple Selection drop box

Number of User Comments : 3

thanks this is working fine
Jnanendra Veer05-09-2012
thanks this is working fine
Working perfectly Dude.Thanks
Post Comment This is for short comments only. Use the forum for more discussions.
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2015 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer