Retaining radio button value of a form after posting

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

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"); 

Sitemap Form Form post back data
Managing listbox in a Form using PHP Checkbox in PHP
PHP sticky form using radio buttons
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer