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>