I want the DropDown text to be retained when the Form returns from POST. How to maintain the state?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You have to set the
selectedattribute on theoptionelement.See http://reference.sitepoint.com/html/option/selected
The
$_POSTarray will either contain the numeric index of the option element or thevalueif this attribute is specified. In the example above,$_POST['favoritefood']contains ‘egg’. You could build a helper that builds the option elements for you, e.g.Of course, it would be smarter to have a Selectbox helper instead which you can pass the options and the POST array in one go, instead of calling it for each option separately. I leave it up to you to build this.
If you are not building your Select options dynamically with PHP, you can select the option by adding a small javascript to your page that you pass the value set for favoritefood from the $_POST array and have the script select the option. See the answers to this question for possible code.