Richard
Richard

Reputation: 6116

PHP Display Error on submission

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.

Upvotes: 0

Views: 130

Answers (3)

Hazem Salama
Hazem Salama

Reputation: 15111

Try this

<script>
    function checkform(){

    var checked = false;
    var buttons = document.getElementsByTagName('input')
    for (var i = 0; i < buttons.length ; i++) {
       if (buttons[i].checked) {
           checked = true;
           break;
       }
   }
   return checked;

   }
</script>

<form onsubmit="return checkform()">
<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" />

Check the fiddle

This should get you started. Please be aware that this script is selecting elements by tag name, which means it will pick up any input in the form. If you are OK with using jQuery I'd highly recommend it, it will make this kind of tasks trivial.

Upvotes: 0

Abraham Brookes
Abraham Brookes

Reputation: 1998

Your setclick() function will always set $clicked to true if the form has been posted. Since you then call setclick() after your main php block, it will be true whether or not the rest of the logic has changed it. Try this:

 if(isset($_POST['Reg_type']) && $_POST['Reg_type'] != ''){ 
    $reg_type = $_POST['Reg_type'];
    header('Location: Registration_2.php');
 } else $clicked = true;

and then down the bottom of the page under the form:

 if($clicked) echo "Please select an option";

Upvotes: 1

FirmView
FirmView

Reputation: 3150

<?php
 $reg_type = $_POST['Reg_type'];

 if($reg_type == "1"){
       //action 1
 } else if($reg_type == "2"){
      //action 2 
 } else if($reg_type == "3") {
     //action 3 
 } else {
    // no radio button is checked. display the message

    $message = "Please select an option";
 }

?>

Upvotes: 0

Related Questions