Back to PHP Scripts List
Remembering the Selected Option of a Select Dropdown Menu When a Form is Posted Back in PHP
Before processing user input from a form, we usually want to use PHP to validate that the input is as expected. If there is an error, we return the user back to the form. Our current preferred technique for PHP form submission is to post it back to itself. When we are using select dropdown menus in the form, we need to make sure that if the user has chosen a particular option, this option gets remembered when the form is re-displayed to the user.
Below we give the full script that demonstrates a technique for remembering the selected option in dropdown. Copy and paste the script including the html tags, and save the file with a .php extension to save it on your server. You can also view the script in action here:
http://www.webref.eu/php-script-1.php
<!-- SCRIPT BEGINS -->
<html>
<head>
<title>PHP Script from WebRef.eu</title>
<meta name="description" content="An example script whereby the selected 0 to 10 rating in a select dropdown will be remembered on form postback, from WebRef.eu">
<meta name="keywords" content="webmaster, web design.">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.txterror {
color: #FF0000;
}
-->
</style>
</head>
<body>
<?php
//If processform flag is set, processing can occur. If not set, form is being loaded for the first time
if ($_POST['processform'] == 1) {
//Process Form
//retrieve form input
$ReviewRating=$_POST['ReviewRating'];
$ReviewDesc=$_POST['txtReviewDesc'];
//Validate fields
// create empty error variable
// always concatenate error variable so you don't wipe out other error messages
$ErrorMsg = "";
If ($ReviewRating == 11) {
$ErrorMsg = $ErrorMsg . "Please select a Rating.<br>";
}
if (!$ReviewDesc) {
$ErrorMsg = $ErrorMsg . "Please give Your Review.<br>";
}
echo "<p class='txterror'>" . $ErrorMsg . "</p>";
//If no errors then form input successful and can move to next step e.g. write to database
If ($ErrorMsg == "") {
echo "<p>Form processing successful.<br><br>Review Rating was: " . $ReviewRating . "<br>Review Description was: " . $ReviewDesc . "</p>";
}
//Close If processform flag is set
}
//If condition for when to display form
//when processform flag is 1 and ErrorMsg is empty we don't want to display the form
//otherwise we display the form for error correction
If (!($_POST['processform'] == 1 && $ErrorMsg == "")) {
?>
If you have a select dropdown in your form, you need whatever option the user selected to be remembered if the form has to be re-displayed because of errors in other fields. In this example, leave the Your Review field blank and submit the form to watch how the Rating field gets remembered.
<br>
<br>
<form name="form1" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
<select name="ReviewRating" id="ReviewRating">
<?php
//If form being shown for the first time
//We give Please Select option a value of 11 to make coding for it easier
If($_POST['processform'] != 1 || $_POST['ReviewRating'] == 11) {
echo "<option value=\"11\" selected=\"selected\">Please Select</option>\n";
}
//loops0 through 10, and when a postback adds the HTML attribute selected to the option tag, if the rating matches the number being printed
for ($i = 0; $i <= 10; $i++) {
//If a postback, check which Review Rating was selected
If($_POST['processform'] == 1) {
$selected = ($ReviewRating == $i) ? ' selected="selected"' : '';
}
echo "<option$selected>$i</option>\n";
}
?>
</select>
<br>
<br>
Your Review (leave this blank to give an error msg and watch how the rating above is remembered):<br>
<textarea name="txtReviewDesc" cols="50" rows="4"><?print $ReviewDesc; ?></textarea>
<br>
<br>
<!-- processform flag so you can tell when form is posted back -->
<input type="hidden" name="processform" value="1">
<input type="submit" name="Submit" value="Submit">
</form>
<?php
//End If condition for when to display form
}
?>
<br>
<a href="index.php">Back to Home Page</a>
</body>
</html>
<!-- SCRIPT ENDS -->
Back to PHP Scripts List
|