PHP Code to Select an Option After a Form Post


I have a couple of php pages with $_POST[] forms which I validate (using PHP). If the form fails validation (ie, the user fails to enter an email address), then the user is brought back to the same page, where he is asked to re-submit the missing or incorrect information. The form also has radio buttons and drop-down forms, and I don’t want to make the user re-select those radio buttons or drop-down entries. So this is my solution:

Re-Select a Drop-Down Entry

#####################################################################
# FUNCTION: Select a previously selected option in a drop-down list #
function option_selected($name, $value) {
$v=$_POST[$name]; // Retrieve the $_POST[] value of the just-submitted drop-down selection
	if ($v == $value) {
		$selected = "selected";
	}
//echo $selected; // use this if you're embedding the function as a php call within an html file.
return $selected; // use this if you want to dump the results to a php variable.
}

This is how to implement the code in PHP/ HTML:

<form action="<?php echo $PHP_SELF;?>" method="post">
Send To:
<select name="sendTo">
<option value="None" <?php echo option_selected("sendTo", "None"); ?>>&mdash;WHOM?&mdash;</option>
<option value="Boss" <?php echo option_selected("sendTo", "Boss"); ?>>A Boss or Supervisor</option>
<option value="Teacher" <?php echo option_selected("sendTo", "Teacher"); ?>>A Teacher</option>
<option value="Client" <?php echo option_selected("sendTo", "Client"); ?>>A Client</option>
<option value="Friend" <?php echo option_selected("sendTo", "Friend"); ?>>A Friend or Peer</option>
<option value="Child" <?php echo option_selected("sendTo", "Child"); ?>>A Child</option>
<option value="Self" <?php echo option_selected("sendTo", "Self"); ?>>Yourself (ie, reading a book)</option>
</select>
</form>


So, if the person selected “Teacher” before submitting the form, then the word “selected” will be printed in the Teacher option, causing it to be pre-selected:

Send To:

Re-Select a Radio Button or Checkbox Entry

The code is almost identical to the function above, except that we use the word “checked” to pre-select a checkbox or radio button:

##########################################################################
# FUNCTION: Select a previously selected option in a radio or check list #
function option_checked($name, $value) {
$v=$_POST[$name];
	if ($v == $value) {
		$checked = "checked";
	}
//echo $selected; // use this if you're embedding the function as a php call within an html file.
return $selected; // use this if you want to dump the results to a php variable.
}

This is how to implement the code in PHP/ HTML:

<?php $gender = $_POST['gender'] ?>
<form action="<?php echo $PHP_SELF;?>" method="post">
Choose Study Partner Gender: 
<input type="radio" name="gender" value="Either" id="either" <?php if (empty($gender)) {echo "checked";} else {echo option_checked("gender", "Either");} ?> /> No Preference <br />
<input type="radio" name="gender" value="Female" id="female" <?php echo option_checked("gender", "Female"); ?> /> Female <br />
<input type="radio" name="gender" value="Male" id="male" <?php echo option_checked("gender", "Male"); ?> /> Male <br /> </form>

Note that I check to see if $gender has been set. If not, I choose a default. This is important because unlike a drop-down, check boxes and radio buttons don’t automatically select a default unless you tell them to. If the user had selected “Female” before submitting the form, she would see this when the page re-loaded:

Choose Study Partner Gender: No Preference
Female
Male

  1. No comments yet.
(will not be published)