Home > Back-end >  Run PHP after JS validation
Run PHP after JS validation

Time:11-20

I am doing email validation for admin registration using JavaScript and save the data to database using PHP. Supposedly, the registration is done only if the email is valid. But when the email evaluates to invalid, the PHP code still run. How do I do it so that when the email is invalid, the PHP won't run.

Below is the PHP code to save data to database:

<?php
include('connection.php');

if(isset($_POST['saveBtn']))
{
    $name    = $_POST['name'];
    $ic      = $_POST['ic'];
    $email   = $_POST['email'];
    $pass    = $_POST['pass'];
    $dob     = $_POST['dob'];
    $contact = $_POST['contact'];
    $gender  = $_POST['gender'];
    $des     = $_POST['des'];
    $address = $_POST['address'];


    // Check if data exist
    $check = "SELECT * FROM admin WHERE admEmail = '".$email."' AND admPassword = '".$pass."'";
    if(mysqli_num_rows(mysqli_query($connect,$check)) > 0)
    {
        ?>
        <script>
            alert('This email and password already registered!');
        </script>
        <?php
    }
    else
    {
        $insert = "INSERT INTO admin (admName, admIC, admEmail, admPassword, admDOB, admContact, admGender, admDesignation, admAddress, admDateJoin) VALUES ('".$name."', '".$ic."', '".$email."', '".$pass."', '".$dob."', '".$contact."', '".$gender."', '".$des."', '".$address."', NOW())";
    
        if(mysqli_query($connect, $insert))
        {
            ?>
            <script>
                alert('Insertion Successful!');

                window.close();
                window.opener.location.reload();
            </script>
            <?php
        }
        else
        {
            ?>
            <script>
                alert('Insertion Failed. Try Again!');
            </script>
            <?php
        }
    }
}
?>

Below is the JS:

function validateEmail() {

            var email = document.addAdminForm.email.value;
            var validRegex = /^[a-zA-Z0-9.!#$%&'* /=?^_`{|}~-] @[a-zA-Z0-9-] (?:\.[a-zA-Z0-9-] )*$/;

            if (email.match(validRegex))
            {
                alert("Valid email address!");
                
                return true;
            }
            else
            {
                document.getElementById("email_error").innerHTML = "Invalid email";

                document.addAdminForm.email.focus();

                return false;
            }

        }

Below is the partial HTML form:

<form  name="addAdminForm" method="POST" onsubmit="validateEmail(this)" action="add_admin.php">
    <div >
        <div >
            <!-- <label for="email">Email</label> -->
            <input type="text"  name="email" placeholder="Email" required>
            <span  id="email_error"></span>
        </div>
    <div >
        <input type="submit"  value="Save" name="saveBtn">
    </div>
</form>

I expect PHP run when validation is true

CodePudding user response:

add this:

onsubmit="return validateEmail(this)"

CodePudding user response:

change your JS code to:

var validRegex = /^([a-zA-Z0-9_-]) @([a-zA-Z0-9_-]) ((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
  • Related