I'm trying to make form validation with ajax. Mine problem is elseif statement doesn't work properly
if (isset($_POST['submit'])){
//Create safe values for input into database
$username = mysqli_real_escape_string($mysqli, $_POST['username']);
$password = mysqli_real_escape_string($mysqli, $_POST['password']);
$company = mysqli_real_escape_string($mysqli, $_POST['company']);
$mail = mysqli_real_escape_string($mysqli, $_POST['mail']);
$adress = mysqli_real_escape_string($mysqli, $_POST['adress']);
$errorEmpty = false;
$errorTaken = false;
$errorMail = false;
//Check are required fields empty
if(empty($username) || empty($password) || empty($company) || empty($mail) || empty($adress)){
echo "<span class='form-error'>Fill in all fields!</span>";
$errorEmpty = true;
}
//Check is username already taken HERE IS THE PROBLEM!
elseif(isset($_POST['username'])){
$username = mysqli_real_escape_string($mysqli, $_POST['username']);
if(!empty($username)){
$username_query = mysqli_query($mysqli, "SELECT * FROM users WHERE user_username = '$username'");
$count=mysqli_num_rows($username_query);
if($count!=0)
{
echo"<span class='form-error'>Username is already taken! </span>";
$errorTaken = true;
}
}
}
//Check is mail adress correct
elseif(!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
echo "<span class='form-error'> Write a valid e-mail adress!</span>";
$errorMail = true;
}
else{
//Create user
$add_user_sql = "INSERT INTO users (user_username, user_password, user_company, user_mail, user_adress)
VALUES ('".$username ."', '".$password."', '".$company."', '".$mail."', '".$adress."')";
$add_user_res = mysqli_query($mysqli, $add_user_sql)
or die(mysqli_error($mysqli));
//Close connection to MySQL
mysqli_close($mysqli);
echo "<span class='form-success'>Succesifuly registrated!</span>";
}
}
Problem is when i check is username already in database,because after that step every next elseif statement won't execute. When I put it on first place it stops execution of rest of code,same situation is when I put it on last place.Other conditions for checking empty fields and mail validation are working properly.
Aucun commentaire:
Enregistrer un commentaire