So I made a registration form such that the user must select one of the three radio options before continuing. However if the user didn’t choose an option and clicked submit I want to display at least a message that says “please choose an option” or something. Here is my code so far:
<?php
$reg_type = "";
function setclick()
{
if(isset($_POST["Submit"]))
$clicked=1;
else
$clicked=0;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Reg_type"])) {
$clicked=1;
header('Location: Registration_1.php');
}
else {
$reg_type = $_POST["Reg_type"];
header('Location: Registration_2.php');
}
}
echo $clicked;
?>
<form name="frmtype" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<input type="radio" name="Reg_type" value="1"/> Registering myself with credit card or bank account <br/>
<input type="radio" name="Reg_type" value="2"/> Registering multiple people using credit card or bank account <br/>
<input type="radio" name="Reg_type" value="3"/> Registering multiple people using a purchase order <br/>
<input type="submit" name="Submit" value="Submit" />
<?php
setclick();
if($clicked)
echo "Please select an option";
?>
I’m having a real hard time getting the logic down to display the error message if they didn’t choose an option and clicked submit.
Your
setclick()function will always set$clickedto true if the form has been posted. Since you then callsetclick()after your main php block, it will be true whether or not the rest of the logic has changed it. Try this:and then down the bottom of the page under the form: