How to not redirect and display the error if the user enter invalid input

574 Views Asked by At

I'm new to PHP and I just want to make some form like a basic form. But I have trouble in the values entered by the user. For example if they enter a empty field it should not redirect or if in the email they didn't enter a correct format the form should not redirect. But in my case it always redirect even though the input is invalid. Can you help me out on how can I stop redirecting to another page if the value entered by the user is invalid?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FG4</title>
</head>

<style>

.error {color: red}

</style>
<body>
    


<?php
        $fname = $lname = $fgender = $mail = $dob = $address = "";

        $fnameErr = $lnameErr = $genderErr = $mailErr = $dobErr = $addressErr = "";

        if ($_SERVER["REQUEST_METHOD"] == "GET") {

        if (empty($_GET["fname"])) {

            $fnameErr = "Please enter your first name.";

        } else {

            $fname = input($_GET["fname"]);

            // check if name only contains letters and space
            if (!preg_match("/^[a-zA-Z-' ]*$/",$fname)) {

            $fnameErr = "Please enter a valid name";

            }

        }

        if (empty($_GET["lname"])) {

            $lnameErr = "Please enter your last name.";

        } else {

            $lname = input($_GET["lname"]);

            // check if name only contains letters and space
            if (!preg_match("/^[a-zA-Z-' ]*$/",$lname)) {

            $lnameErr = "Please enter a valid name";

            }

        }

        if (empty($_GET["gender"])) {

            $genderErr = "Please select a gender.";

        } else{ 
            $gender = input($_GET["gender"]);
        }

        if (empty($_GET["mail"])) {

            $mailErr = "Please enter your email.";

        } else {

            $mail = input($_GET["mail"]);

            // check if email contain gmail.com or yahoo.com
            if (!preg_match("/@gmail.com|@yahoo.com/", $mail)) {

            $mailErr = "Please enter a valid email.";            

            }
        }

        if (empty($_GET["dob"])) {

            $dobErr = "Please select your date of birth.";

        } else{

            $lname = input($_GET["lname"]);
        }
        
        if (empty($_GET["address"])) {

        $addressErr = "Please enter your address.";

        } else {

        $address = input($_GET["address"]);

        // check if address contain the following characters
        if (!preg_match(" /#|[0-9]|[a-z]|[A-Z]/ ",$address)) {

        $address = "Please enter a valid address";
            }

        }
        }

        function input($data) {

        $data = trim($data);

        $data = stripslashes($data);

        $data = htmlspecialchars($data);

        return $data;

        }

?>

<form method="get" action="trial.php">  

        First Name: <input type="text" name="fname">
        <span class="error">* <?php echo $fnameErr;?></span>
        <br><br>

        Larst Name: <input type="text" name="lname">
        <span class="error">* <?php echo $lnameErr;?></span>
        <br><br>

        Gender: <input type="radio" name="gender" value="male"> Male
                <input type="radio" name="gender" value="female"> Female
        <span class="error">* <?php echo $genderErr;?></span>
        <br><br>

        Email: <input type="text" name="mail">
        <span class="error">* <?php echo $mailErr;?></span>
        <br><br>

        Date of Birth: <input type="date" name="dob">
        <span class="error">* <?php echo $dobErr;?></span>
        <br><br>

        Address: <br><textarea type="text" name="address" rows="5" cols="40"></textarea>
        <span class="error"><?php echo $addressErr;?></span>
        <br><br>

        <input type="submit" name="submit" value="Submit">  

</form>

Here is the other code where it just print the values entered by the user

<?php


if ($_SERVER["REQUEST_METHOD"] == "GET") {
    // collect value of input field
    $fname = $_GET['fname']; 
    $lname = $_GET['lname']; 
    $gender = $_GET['gender']; 
    $mail = $_GET['mail']; 
    $dob = $_GET['dob']; 
    $address = $_GET['address']; 
       
    
echo "<h2> Final Output:</h2>";
echo "First Name :$fname";
echo "<br>";
echo "Last Name :$lname";
echo "<br>";
echo "Gender  :$gender";
echo "<br>";
echo "Email :$mail";
echo "<br>";
echo "Date of Birth :$dob";
echo "<br>";
echo "Address :$address";

}
?>


</body>
</html>
1

There are 1 best solutions below

0
Ken Lee On

There are many ways to do what you want.

One of them is to use a hidden form and submit it only if there is no error found after the validation.

Hence the amended code (based on your original code) will be:

trial.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FG4</title>
</head>

<style>

.error {color: red}

</style>
<body>
    


<?php
        $fname = $lname = $fgender = $mail = $dob = $address = "";

        $fnameErr = $lnameErr = $genderErr = $mailErr = $dobErr = $addressErr = "";

        if ($_SERVER["REQUEST_METHOD"] == "GET") {

        if (empty($_GET["fname"])) {

            $fnameErr = "Please enter your first name.";

        } else {

            $fname = input($_GET["fname"]);

            // check if name only contains letters and space
            if (!preg_match("/^[a-zA-Z-' ]*$/",$fname)) {

            $fnameErr = "Please enter a valid name";

            }

        }

        if (empty($_GET["lname"])) {

            $lnameErr = "Please enter your last name.";

        } else {

            $lname = input($_GET["lname"]);

            // check if name only contains letters and space
            if (!preg_match("/^[a-zA-Z-' ]*$/",$lname)) {

            $lnameErr = "Please enter a valid name";

            }

        }

        if (empty($_GET["gender"])) {

            $genderErr = "Please select a gender.";

        } else{ 
            $gender = input($_GET["gender"]);
        }

        if (empty($_GET["mail"])) {

            $mailErr = "Please enter your email.";

        } else {

            $mail = input($_GET["mail"]);

            // check if email contain gmail.com or yahoo.com
            if (!preg_match("/@gmail.com|@yahoo.com/", $mail)) {

            $mailErr = "Please enter a valid email (only @gmail.com or @yahoo.com).";            

            }
        }

        if (empty($_GET["dob"])) {

            $dobErr = "Please select your date of birth.";

        } else{

            $lname = input($_GET["lname"]);
        }
        
        if (empty($_GET["address"])) {

        $addressErr = "Please enter your address.";

        } else {

        $address = input($_GET["address"]);

        // check if address contain the following characters
        if (!preg_match(" /#|[0-9]|[a-z]|[A-Z]/ ",$address)) {

        $address = "Please enter a valid address";
            }

        }
        }

        function input($data) {

        $data = trim($data);

        $data = stripslashes($data);

        $data = htmlspecialchars($data);

        return $data;

        }

?>

<form id="form_id" method="get"  action=#>  

        First Name: <input type="text" name="fname" value="<?php echo $_GET["fname"];?>">
        <span class="error">* <?php echo $fnameErr;?></span>
        <br><br>

        Larst Name: <input type="text" name="lname"  value="<?php echo $_GET["lname"];?>">
        <span class="error">* <?php echo $lnameErr;?></span>
        <br><br>

        Gender: <input type="radio" name="gender" value="male" 
<?php if ($_GET["gender"]=="male") { echo " checked ";} ?>
        > Male
                <input type="radio" name="gender" value="female"
<?php if ($_GET["gender"]=="female") { echo " checked ";} ?>
                > Female
        <span class="error">* <?php echo $genderErr;?></span>
        <br><br>

        Email: <input type="text" name="mail" value="<?php echo $_GET["mail"];?>">
        <span class="error">* <?php echo $mailErr;?></span>
        <br><br>

        Date of Birth: <input type="date" name="dob" value="<?php echo $_GET["dob"];?>">
        <span class="error">* <?php echo $dobErr;?></span>
        <br><br>

        Address: <br><textarea type="text" name="address" rows="5" cols="40"><?php echo $_GET["address"];?></textarea>
        <span class="error"><?php echo $addressErr;?></span>
        <br><br>

        <input type="submit" name="submit" value="Submit">  

</form>




<?php 
if ($fnameErr=="" && $lnameErr==""  && $genderErr=="" && $mailErr==""  && $dobErr=="" && $addressErr=="") { ?>
<form id="form_id2" method=GET action="trial2.php">
<input type=hidden name="fname" value="<?php echo $_GET["fname"];?>">
<input type=hidden name="lname" value="<?php echo $_GET["lname"];?>">
<input type=hidden name="gender" value="<?php echo $_GET["gender"];?>">
<input type=hidden name="mail" value="<?php echo $_GET["mail"];?>">
<input type=hidden name="dob" value="<?php echo $_GET["dob"];?>">
<textarea name=address style="display:none;"><?php echo $_GET["address"];?></textarea>
</form>

<script>
document.getElementById("form_id2").submit();
</script>    

<?php } ?>

trial2.php

<?php


if ($_SERVER["REQUEST_METHOD"] == "GET") {
    // collect value of input field
    $fname = $_GET['fname']; 
    $lname = $_GET['lname']; 
    $gender = $_GET['gender']; 
    $mail = $_GET['mail']; 
    $dob = $_GET['dob']; 
    $address = $_GET['address']; 
       
    
echo "<h2> Final Output:</h2>";
echo "First Name :$fname";
echo "<br>";
echo "Last Name :$lname";
echo "<br>";
echo "Gender  :$gender";
echo "<br>";
echo "Email :$mail";
echo "<br>";
echo "Date of Birth :$dob";
echo "<br>";
echo "Address :$address";

}
?>


</body>
</html>