0

I am trying to validate my signup form but I keep getting an undefined variable in:

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

When I move the variables in my php code to the global scope, I get no errors but instead of being sent to the user profile page, I am sent to a blank page with /include/signup.inc.php in the url. I can't figure out why it keeps sending me to a blank page. Any help would be greatly appreciated. Thank you.

html code:

<!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 code:

<?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>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

1 Answers1

0

try this code. i think it will help you. first assign php variable for both

$errorEmpty = 0;
 $errorEmail = 0;

which you can access from script then update it if condition not exit then use it in script . i just update true and false using 0 and 1. hope it will help you but not tested

  <?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>
Shafiqul Islam
  • 5,570
  • 2
  • 34
  • 43