Radio buttons with the same name submit one selected value. Validate against an allow-list, then add checked only to the matching option.
$allowed = ['monthly', 'yearly'];
$plan = $_POST['plan'] ?? '';
if (!in_array($plan, $allowed, true)) {
$plan = '';
}
echo '<input type="radio" name="plan" value="monthly"' . ($plan === 'monthly' ? ' checked' : '') . '> Monthly';
This is the recommended starting pattern. The detailed examples below are retained for explanation, comparison and related variations.
$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");
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.