SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

Retaining radio button value of a form after posting

Learn Details on retaining all post back values of a form

Period or radio buttons are used where the visitor has to make selection out of some given choice. The user can only make one choice out of the given options. So radio buttons are grouped in a common choice and one of them is selected by the user. The difference between radio button and check box is user can make multiple selections in case of check boxes.

A group of radio buttons will have common name and different values. Here to retain the value of the radio buttons we have to read the value of the button. We will use header redirection to post back the value of the period button to the main page or the page where form is used. Once the member makes a selection and submits the form, the data posted to another page name pb-prdck.php and post it back to our main page using redirection. Here we assumed that all form validation is failed so we are posting back the data. Here is the code to display the form, note that we have used one unique variable for each period button and the variables gets their values from a switch command which check value of the period button and accordingly set and reset the variables.

Here is the code of the form with decode script.

$t1=$_GET['t1'];
switch($t1)
{
case "select 1":
$t1v="checked";
$t2v="";
$t3v="";
$t4v="";
break;

case "select 2":
$t1v="";
$t2v="checked";
$t3v="";
$t4v="";
break;

case "select 3":
$t1v="";
$t2v="";
$t3v="checked";
$t4v="";
break;

case "select 4":
$t1v="";
$t2v="";
$t3v="";
$t4v="checked";
break;

default: // By default the 4th selection is selected
$t1v="";
$t2v="";
$t3v="";
$t4v="checked";
break;

}

echo "
<form method=post action=pb-prdck.php>
<input type=radio name=t1 value='select 1' $t1v>Select 1 |
<input type=radio name=t1 value='select 2' $t2v>Select 2 |
<input type=radio name=t1 value='select 3' $t3v>Select 3 |
<input type=radio name=t1 value='select 4' $t4v>Select 4 |

<input type=submit value=Submit>
";


Here is the code for pb-prdck.php file

$t1=$_POST[t1];
header ("Location: pb-prd.php?t1=$t1");


Here is the demo of the script. Select one radio button and submit.

Select 1 | Select 2 | Select 3 | Select 4 |


Discuss this tutorial at forum


Further readings
All form components validation with post back and data locking
Checkbox
How to retain form data if validation fails?
How to retain data of a text box after submitting
Retaining status of a check box
Updating checkbox value from & to a table record.
Retaining selected data of a period button of a form
Retaining selected data of a drop down list box of a form
Validating Date: Checking if date exist
Email address validation in a form
PHP Form Validation
is_numeric to check numeric numbers
ctype_alnum to check alphanumeric characters data
ctype_alpha to check alphabetic characters data
How to take care of form & query string variables if Register global is OFF?














 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.