Sam Adams
Sam Adams

Reputation: 21

Validation Of Email Failure - PHP

I'm attempting to validate an Email through PHP. I'm very new to PHP so I apologize in advance if its something very simple.

My page is loading without an error but I can continue to submit the form with a wrong formatted email. I would like the page to refresh and a message pop up giving the user a prompt to use a valid email address.

Thanks In Advance

<?php
    session_start();

    require 'database.php';

    $message = '';

    if (isset($_POST['email']) == true && empty($_POST['email']) == false) {
        $email = $_POST['email'];

        if(filter_var($email, FILTER_VALIDATE_EMAIL) == true){
            echo 'That\'s a valid email address';
        } else {
            echo'Not a valid email address';
        }
    }
    if(!empty($_POST['email']) && !empty($_POST['phonenumber']) && !empty($_POST['postcode']) && !empty($_POST['Name'])):

        $stmt = $conn->prepare('SELECT email FROM noodles_gamification WHERE email = :email');
        $stmt->execute(array(':email' => $_POST['email']));
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if(!empty($row['email'])){
            $message = 'email provided is already in use.';
        } else {

            $sql = "INSERT INTO noodles_gamification (email, phonenumber, postcode, Name) VALUES (:email, :phonenumber, :postcode, :Name)";
            $stmt = $conn->prepare($sql);

            $stmt->bindParam(':email', $_POST['email']);

            $stmt->bindParam(':phonenumber', $_POST['phonenumber']);
            $stmt->bindParam(':postcode', $_POST['postcode']);
            $stmt->bindParam(':Name', $_POST['Name']);

            if( $stmt->execute() ){
                $_SESSION['email'] =  $_POST['email'];
                header("Location: ../");
            }
        }
    endif;
?>

<!DOCTYPE html>
<html>
<head>
<title>Register</title>
<link rel="stylesheet" stype="text/css" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Comfortaa" rel="stylesheet">

</head>

<body>
<div class="header">
  <a href="../index.php"> Your App Name</a>
</div>

<?php if(!empty($message)): ?>
    <p><?= $message ?></p>
<?php endif; ?>

<h1>Need a few details before you start the game</h1>
    <form action="register.php" method="POST">
    <input type="text" placeholder="Enter Your Full Name" name="Name">
        <input type="text" placeholder="Enter Your Email" name="email">
        <input type="text" placeholder="Phone Number" name="phonenumber">
        <input type="text" placeholder="Post Code" name="postcode">

    <input type="submit">
</body>
</html>

Upvotes: 1

Views: 41

Answers (2)

Sumanth Mungari
Sumanth Mungari

Reputation: 11

The simple way to validate an email in PHP will be as follows,

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { // invalid emailaddress }

and you could refer the link How to validate an Email in PHP

Thank you.

Upvotes: 1

RAUSHAN KUMAR
RAUSHAN KUMAR

Reputation: 6006

You are writing this code which validate the email

if (isset($_POST['email']) == true && empty($_POST['email']) == false) {
        $email = $_POST['email'];
        if(filter_var($email, FILTER_VALIDATE_EMAIL) == true){
            echo 'That\'s a valid email address';

        } else {
            echo'Not a valid email address';

        }
    }

But after that validating you are displaying message and your script goes further to execute. Whereas you should stop after displaying the email error. So use this

$emailError=false;
if (isset($_POST['email']) == true && empty($_POST['email']) == false) {
        $email = $_POST['email'];
        if(filter_var($email, FILTER_VALIDATE_EMAIL) == true){
            echo 'That\'s a valid email address';

        } else {
            echo'Not a valid email address';
            $emailError=true;

        }
    }

Then check if the emailError is false then goes further

if(!$emailError){
   //your code to save the data or manipulate the data
}

Upvotes: 1

Related Questions