SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP Form Form post back

Retaining radio button value of a form after posting

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.

demo of the radio button script.

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-demo.php?t1=$t1");


Google+
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer