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>
 
     
     
     
    