简体   繁体   English

为什么我一直在我的脚本标签中获取未定义的变量:errorEmpty和errorEmail?

[英]Why do I keep getting Undefined variable: errorEmpty and errorEmail in my script tags?

Why do I keep getting Undefined variable: errorEmpty and errorEmail in my script tags? 为什么我一直在我的脚本标签中获取未定义的变量:errorEmpty和errorEmail?

i'm trying to validate a sign up form using ajax. 我正在尝试使用ajax验证注册表单。 but I keep getting an undefined variable error in: 但我不断收到一个未定义的变量错误:

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

any advice will will greatly appreciated. 任何建议都将非常感谢。 thank you. 谢谢。

<?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='form-error'>Please fill out all fields!</span>";
 $errorEmpty == true;
}

elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
 echo "<span class='form-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 "Email address already exists";
 header("Location: ../index.php?error=email");
 exit();
}


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);header("Location: ../profile.php");

    }

}

} 


?>


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

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

 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>

Define $errorEmpty and $errorEmail in the top of the page above this 在此页面顶部定义$errorEmpty$errorEmail

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

}

== is comparision operators. ==是比较运算符。 To assign value to any variable simply use single = 要为任何变量赋值,只需使用single =

<?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='form-error'>Please fill out all fields!</span>";
 $errorEmpty = true;
}

elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
 echo "<span class='form-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 "Email address already exists";
 header("Location: ../index.php?error=email");
 exit();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM