Why is my email check variable returning an error as undefined

user7951652

I am validating my signup form using ajax. Can someone please answer why I am getting an error: Undefined variable: emailcheck. it is clearly defined in the line before it. Thank you. Here is the code below.

<?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>
Amr Aly

You can do something like :

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

    }

}

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is my variable tabledata returning undefined?

Why is my variable returning undefined in javascript?

Why is my variable returning as 'undefined' despite being set a value?

why my Nodejs / NestJs promise is returning undefined to variable in second function?

Why is my JavaScript hoisted local variable returning undefined but the hoisted global variable is returning blank?

why is my object returning undefined?

Why is my promise returning undefined?

Why is my intersectionObserver returning my querySelect as undefined?

my variable is returning empty why?

why does my functions returning "undefined"

Why is my recursive function returning undefined?

Why is my reducer returning undefined, React TypeScript

Why is my function returning an array with undefined as items?

why does my nodejs function returning undefined?

Why is my EJS variable returning "undefined" when i'm trying to use the mongoose "findByIdAndRemove" method

Why is the variable argument of this required function returning undefined?

Why is my imported variable undefined?

RSpec assigns returning undefined local variable error

Why is error flagging the variable as undefined?

Why is my tableView variable returning nil?

Why is my Swift string variable returning nil?

Why is my function returning 1 and not the variable value?

Why is my get request returning undefined in my image file tree?

Why is my axios post returning undefined in my functional component?

Why is UserManager.Find() returning null if my UserName is email based?

Why is my implementation of IExceptionHandler not returning the error response?

Not sure why my function keeps returning an error?

Why is my count returning a syntax error?

Why is my code returning an error for this problem?