I would like to know how to stop form submission when the validation fails?

22,215

Solution 1

If you want to print all the errors, so your code should be like below...

<?php session_start(); error_reporting(0);?>
<!DOCTYPE HTML> 
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body> 
<?php

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
   if (empty($_POST["name"]))
     {$_SESSION['name']= "Name is required";}
   if (empty($_POST["email"]))
     {$_SESSION['email'] = "Email is required";}
   if (empty($_POST["website"]))
     {$_SESSION['website'] = "Website is required";}
   if (empty($_POST["comment"]))
     {$_SESSION['comment'] = "comment is required";}
   if (empty($_POST["gender"]))
     {$_SESSION['gender'] = "Gender is required";}
}
if($_POST['name']!="" && $_POST['email']!="" && $_POST['website']!="" && 

$_POST['gender']!="")
{
    header("Location: welcome.php");
}
?>

<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action=""> 
   <label>Name:</label> <input type="text" name="name"> <span class="error">* <?php echo $_SESSION['name'];?></span>
   <br><br>
   <label>E-mail:</label> <input type="text" name="email"> <span class="error">* <?php echo $_SESSION['email'];?></span>
   <br><br>
   <label>Website:</label> <input type="text" name="website"> <span class="error"><?php echo $_SESSION['website'];?></span>
   <br><br>
   <label>Comment:</label> <input type="text" name="comment">
   <br><br>
   <label>Gender:</label>
   <input type="radio" name="gender" value="female">Female
   <input type="radio" name="gender" value="male">Male
   <span class="error">* <?php echo $_SESSION['gender'];?></span>
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>
</body>
</html>
<?php 
    unset($_SESSION['name']);
    unset($_SESSION['email']);
    unset($_SESSION['website']);
    unset($_SESSION['comment']);
    unset($_SESSION['gender']);
?>

If you want to access all the variables in Welcome page. just code like below

home.php

<?php session_start(); error_reporting(0);?>
<!DOCTYPE HTML> 
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body> 


<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="welcome.php"> 
   <label>Name:</label> <input type="text" name="name"> <span class="error">* <?php echo $_SESSION['name'];?></span>
   <br><br>
   <label>E-mail:</label> <input type="text" name="email"> <span class="error">* <?php echo $_SESSION['email'];?></span>
   <br><br>
   <label>Website:</label> <input type="text" name="website"> <span class="error"><?php echo $_SESSION['website'];?></span>
   <br><br>
   <label>Comment:</label> <input type="text" name="comment">
   <br><br>
   <label>Gender:</label>
   <input type="radio" name="gender" value="female">Female
   <input type="radio" name="gender" value="male">Male
   <span class="error">* <?php echo $_SESSION['gender'];?></span>
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>
</body>
</html>
<?php 
    unset($_SESSION['name']);
    unset($_SESSION['email']);
    unset($_SESSION['website']);
    unset($_SESSION['comment']);
    unset($_SESSION['gender']);
?>

welcome.php

<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
   if (empty($_POST["name"]))
     {$_SESSION['name']= "Name is required";}
   if (empty($_POST["email"]))
     {$_SESSION['email'] = "Email is required";}
   if (empty($_POST["website"]))
     {$_SESSION['website'] = "Website is required";}
   if (empty($_POST["comment"]))
     {$_SESSION['comment'] = "comment is required";}
   if (empty($_POST["gender"]))
     {$_SESSION['gender'] = "Gender is required";}

}
if(empty($_POST["name"]) || empty($_POST["email"]) || empty($_POST["website"]) || empty($_POST["gender"]))
{
    header("Location: home.php");
}

echo $_POST['name'];
?>

Solution 2

If you want to validate your data before you submit,you should be using javascript more specifically jquery to validate the data client side itself,

Give the form an id like this

method="post" action="welcome.php" id="form1"

and ids to all your form elements


$('#form1').submit(function() {
     your validation rules here

     if($('#email').val().length == 0)
       return false;
    else 
       return true;
});

return false stops the submission.

If you are going to do front end and not just php you should really have a go at jquery will make your life easier

Solution 3

Javascript is client side, PHP is server side so until the submit button is not pressed to "Post data to server" and from there you use php to validate the form .. check fields do different operations like database inserts, calculations etc, you cannot send a response back to the client and tell him you here mate got this error i ain't going to work with this kind of data. Well, you could use ajax to live validate the form on server side. the best way to do is to validate client side and then before you use all that data that comes from the client who always lies because everybody lies you do another checking on server. Here is an example.

Share:
22,215
Shashank
Author by

Shashank

Updated on July 09, 2022

Comments

  • Shashank
    Shashank almost 2 years

    I am new to php here's the my code.

    when any of the field is empty i want to stop form submission...

    <!DOCTYPE HTML> 
    <html>
    <head>
    <style>
    .error {color: #FF0000;}
    </style>
    </head>
    <body> 
    
    <?php
    // define variables and set to empty values
    $nameErr = $emailErr = $genderErr = $websiteErr = "";
    $name = $email = $gender = $comment = $website = "";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
    
       if (empty($_POST["name"]))
         {$nameErr = "Name is required";}
       else if (empty($_POST["email"]))
         {$emailErr = "Email is required";}
       else if (empty($_POST["website"]))
         {$website = "";}
       else if (empty($_POST["comment"]))
         {$comment = "";}
       else if (empty($_POST["gender"]))
         {$genderErr = "Gender is required";}
    
    }
    ?>
    
    <h2>PHP Form Validation Example</h2>
    <p><span class="error">* required field.</span></p>
    <form method="post" action="welcome.php"> 
       <label>Name:</label> <input type="text" name="name"> <span class="error">* <?php echo $nameErr;?></span>
       <br><br>
       <label>E-mail:</label> <input type="text" name="email"> <span class="error">* <?php echo $emailErr;?></span>
       <br><br>
       <label>Website:</label> <input type="text" name="website"> <span class="error"><?php echo $websiteErr;?></span>
       <br><br>
       <label>Comment:</label> <input type="text" name="comment">
       <br><br>
       <label>Gender:</label>
       <input type="radio" name="gender" value="female">Female
       <input type="radio" name="gender" value="male">Male
       <span class="error">* <?php echo $genderErr;?></span>
       <br><br>
       <input type="submit" name="submit" value="Submit"> 
    </form>
    </body>
    </html>