为什么我的电子邮件检查变量返回错误为未定义

时间:2017-05-20 06:56:33

标签: php jquery ajax

我正在使用ajax验证我的注册表单。有人可以回答我为什么会收到错误:未定义的变量:emailcheck。它在前面的行中清楚地定义了。谢谢。这是下面的代码。

<?php  
session_start();
include '../dbh.php';




if (isset($_POST['submit'])) {

    $first = $_POST['first'];
    $last = $_POST['last'];
    $email = $_POST['email'];
    $pwd = $_POST['pwd'];

    $errorEmpty = false;
    $errorEmail = false;



if (empty($first) || empty($last) || empty($email) || empty($pwd)) {
    echo "<span class='signup-error'>Please fill out all fields!</span>";
    $errorEmpty = true;
    }

elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "<span class='signup-error'>Please enter a valid email address!</span>";
    $errorEmail = true;
    } 


else {
    $sql = "SELECT email FROM user WHERE email='$email'";
    $result = mysqli_query($conn, $sql);
    $emailcheck = mysqli_num_rows($result);
}


if ($emailcheck > 0) {
    echo "<span class='signup-error'>That email address already exists!</span>";
    $errorEmail = true;
    }



else {
    $encryptpwd = password_hash($pwd, PASSWORD_DEFAULT);
    $sql = "INSERT INTO user (first, last, email, pwd)
    VALUES ('$first', '$last', '$email', '$encryptpwd')";
    $result = mysqli_query($conn, $sql);

    }

}


?>


<script> 
$("#signup-first, #signup-last, #signup-email, #signup-pwd").removeClass("input-error");

    var errorEmpty = "<?php echo $errorEmpty; ?>";
    var errorEmail = "<?php echo $errorEmail; ?>"; 

 if (errorEmpty == true) {
  $("#signup-first, #signup-last, #signup-email, #signup-pwd").addClass("input-error");
 }

 if (errorEmail == true) {
  $("#signup-email").addClass("input-error");
 } 

 if (errorEmpty == false && errorEmail == false) {
  $("#signup-first, #signup-last, #signup-email, #signup-pwd").val("");
}

 </script>

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

if (isset($_POST['submit'])) {

    $first = $_POST['first'];
    $last = $_POST['last'];
    $email = $_POST['email'];
    $pwd = $_POST['pwd'];

    $errorEmpty = false;
    $errorEmail = false;



if (empty($first) || empty($last) || empty($email) || empty($pwd)) {
    echo "<span class='signup-error'>Please fill out all fields!</span>";
    $errorEmpty = true;
}

elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "<span class='signup-error'>Please enter a valid email address!</span>";
    $errorEmail = true;
} 


else {
    $sql = "SELECT email FROM user WHERE email='$email'";
    $result = mysqli_query($conn, $sql);
    $emailcheck = mysqli_num_rows($result);

    if ($emailcheck > 0) {
    echo "<span class='signup-error'>That email address already exists!</span>";
    $errorEmail = true;
    }



else {
    $encryptpwd = password_hash($pwd, PASSWORD_DEFAULT);
    $sql = "INSERT INTO user (first, last, email, pwd)
    VALUES ('$first', '$last', '$email', '$encryptpwd')";
    $result = mysqli_query($conn, $sql);

    }

}

}