未定义的变量和空白页面

时间:2017-05-16 16:57:11

标签: php jquery ajax

我正在尝试验证我的注册表单,但我一直在获取一个未定义的变量:

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

当我将我的php代码中的变量移动到全局范围时,我没有收到任何错误但是没有被发送到用户配置文件页面,我被发送到一个空白页面,其中包含/include/signup.inc.php网址。我无法弄清楚为什么它一直把我送到空白页面。任何帮助将不胜感激。谢谢。

html代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Yahbang</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("signup-form").submit(function(event) {
                event.preventDefault();
                var first = $("#signup-first").val();
                var last = $("#signup-last").val();
                var email = $("#signup-email").val();
                var pwd = $("#signup-pwd").val();
                var submit = $("#signup-submit").val();
                $(".signup-message").load("../signup.inc.php", {
                    first: first,
                    last: last,
                    email: email,
                    pwd: pwd,
                    submit: submit
                });
            });
        });
    </script>


<form id="signup-form" class="signup" action='include/signup.inc.php' method='POST'>
                    <input id="signup-first" type='text' name='first' placeholder='First Name'><br>
                    <input id="signup-last" type='text' name='last' placeholder='Last Name'><br>
                    <input id="signup-email" type='text' name='email' placeholder='Email'><br>
                    <input id="signup-pwd" type='password' name='pwd' placeholder='Password'><br>
                    <button id="signup-submit" type='submit'>Sign Up</button>
                    <p class="signup-message"></p>
                </form>

php代码:

<?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);

        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>

1 个答案:

答案 0 :(得分:0)

试试这段代码。我认为它会对你有所帮助。首先为两者分配php变量

$errorEmpty = 0;
 $errorEmail = 0;

您可以从脚本访问,然后在条件不退出时更新它,然后在脚本中使用它。我只是使用0和1更新true和false。希望它能帮到你,但不会测试

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

 $errorEmpty = 0;
 $errorEmail = 0;
if (isset($_POST['submit'])) {

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



    if (empty($first) || empty($last) || empty($email) || empty($pwd)) {
        echo "<span class='signup-error'>Please fill out all fields!</span>";
        $errorEmpty = 1;
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "<span class='signup-error'>Please enter a valid email address!</span>";
        $errorEmail = 1;
    } 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 = 1;
    } 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 == 1) {
  $("#signup-first, #signup-last, #signup-email, #signup-pwd").addClass("input-error");
 }

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

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

 </script>