Validating and retaining data in Radio buttons, Check boxes and Select boxes

Posted in: PHP by musiitwa joseph 2015-07-11 at 09:02:55

image for Validating and retaining data in Radio buttons, Check boxes and Select boxes Radio buttons are round buttons by default that are used to obtain a yes-no or true-false answer. Users select a single option from a list of choices. When a group of radio buttons share the same name, only one of them can be selected. Once one radio button has been selected, if the user clicks another option, the new option is selected and the old one is deselected.

Check boxes are rectangular shaped that allow a user to select one or more than one option. They are similar to radio buttons in that they don't allow users to enter input but they can be turned on and off by clicking on them.

Select boxes are also known as a pull downs or a drop downs. They are rectangular shaped, allow the user to select one or more option from the defined list.

Radio buttons

<input type="radio" value="" name=""/>

The name and value attributes are very important in processing data. To form a group of radio buttons, the radio buttons should have the same name for example.
<input type="radio" value="male" name="sex"/>
<input type="radio" value="female" name="sex"/>

For an option of a radio button to appear checked by default an attribute checked is added which is equated to checked
<input type="radio" value="" name="" checked="checked"/>

Retaining values checked during validation in radio button is by making an option checked. This is done by using an if else statement or just an if statement, comparing the variable from the super global to option value. lets use an example of sex which is either male or female.

if($sex == "m")
echo '<input type="radio" value="m" name="sex" checked="checked"/> Male';
echo '<input type="radio" value="m" name="sex"/> Male';

if($sex == "f")
echo '<input type="radio" value="f" name="sex" checked="checked"/> Female';
echo '<input type="radio" value="f" name="sex"/> Female';


<input type="radio" value="m" name="sex" <?php if($sex == "m"){ echo 'checked="checked" '; }?> /> Male
<input type="radio" value="f" name="sex" <?php if($sex == "f"){ echo 'checked="checked" '; }?> /> Female

Check boxes

<input type="checkbox" value="" name=""/>

Retaining checkbox checked is also the same as a radio button

<input type="checkbox" value="yes" name="agree" <?php if($agree== "yes"){ echo 'checked="checked" '; }?> /> I agree

Submitting two more values from a group of checkboxes can be done by using an array. Its the name of the checkbox that is changed into an array, If this is not done the last checkbox's value to be checked is the one that will be submitted.

<input name="level[]" type="checkbox" value="Java" <?php for($i=0; $i < count($level); $i++){ if(@$level[$i] == "Java"){ echo 'checked="checked"'; }}?>/> Java

<input name="level[]" type="checkbox" value="Android" <?php for($i=0; $i < count($level); $i++){ if(@$level[$i] == "Android"){ echo 'checked="checked"'; }}?>/> Android

<input name="level[]" type="checkbox" value="Python" <?php for($i=0; $i < count($level); $i++){ if(@$level[$i] == "Python"){ echo 'checked="checked"'; }}?>/> Python

Select boxes


<select name="">
<option value="Option1">Option1</option>
<option value="Option2">Option2</option>
<option value="Option3">Option3</option>

Retaining select box selected is also the same as a radio button

<select name="age">
<option value="">Select</option><!-- a blank option, forces the user to make a selection before submitting -->
<option value="below 17 years" <?php if($age == "below 17 years"){ echo 'selected'; } ?> >below 17 years</option>
<option value="between 17 and 25 years" <?php if($age == "between 17 and 25 years"){ echo 'selected'; } ?> >between 17 and 25 years</option>
<option value="above 25 years" <?php if($age == "above 25 years"){ echo 'selected'; } ?> >above 25 years</option>

Multiple values can be sent from a select box by adding multiple="multiple" in the select opening tag and also change the name of the select box to an array. A blank option is not required. Retaining a value in this case is like retaining a value in a check box.

<select name="array_name[]" multiple="multiple">
- - -