即使验证失败,电子邮件验证仍会继续到下一页 - PHP

时间:2016-11-02 02:52:20

标签: php validation

我在表单中验证我的电子邮件时遇到了一些麻烦。我似乎正在验证它,但它继续到下一页?我假设我只需要在回复“不是有效的电子邮件地址”后停止脚本。我不确定如何做到这一点。

我是PHP的新手,很抱歉,如果它非常简单。

先谢谢

<?php

session_start();
        require 'database.php';

        $message = '';



 $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;

        }

    }
        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>

2 个答案:

答案 0 :(得分:0)

如果无效,请使用exit

 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';exit();
        $emailError=true;

    }

}

答案 1 :(得分:0)

尝试在电子邮件验证后执行代码之前设置IF条件。

if(!$emailError){
//sql's and redirections
}