How to confirm if user exists with those "green ticks"

0

They say it's with javascript, but I searched the net and did not think anything related ...

Example of how I want it to stay:

Currently I do with Alerts, but I would like validation to leave a green light like that of the image (see right corner) on success and red on failure.

What I have so far:

if(isset($_POST["botao"])) {
$name        =  mysqli_real_escape_string($mysqli, $_POST["name"]);
$surname     =  mysqli_real_escape_string($mysqli, $_POST["surname"]);
$birthday    =  mysqli_real_escape_string($mysqli, $_POST["birthday"]);
$sex         =  mysqli_real_escape_string($mysqli, $_POST["sex"]);
$username    =  mysqli_real_escape_string($mysqli, $_POST["username"]);
$email       =  mysqli_real_escape_string($mysqli, $_POST["email"]);
$password    =  mysqli_real_escape_string($mysqli, $_POST["password"]);
$repassword  =  mysqli_real_escape_string($mysqli, $_POST["repassword"]);

if($name == "" || $surname == "" || $birthday == "" || $sex == "" || $username == "" || $email == "" || $password == "" || $repassword == "") {
echo "<script> alert('Preencha todos os campos.'); </script>";
return true;
}
if($password != $repassword) {
echo "<script> alert('As senhas devem ser iguais!'); </script>";
return true;
}
// FAZ A BUSCA NO BDD PRA VER SE O EMAIL JA EXISTE
$select = $mysqli->query("SELECT * FROM data WHERE email='$email'");
if(select) {
$row = $select->num_rows;
if ($row > 0) {
echo "<script> alert('Já existe um usuário com este e-mail!'); </script>";
} else {
// FAZ A BUSCA NO BDD PRA VER SE O USUARIO JA EXISTE
$select = $mysqli->query("SELECT * FROM data WHERE username='$username'");
if(select) {
$row = $select->num_rows;
if ($row > 0) {
echo "<script> alert('O Usuário já existe.'); </script>";
} else {
// CASO TUDO ESTIVER OK ELE INSERE AS INFORMAÇÕES NO BDD
$insert = $mysqli->query("INSERT INTO 'data'('name', 'surname', 'birthday', 'sex', 'username', 'email', 'password', 'status') VALUES ('$name', '$surname', '$birthday', '$sex', '$username', '$email', '".md5($password)."', '0')");
if($insert) {
echo "<script> alert('Usuario registrado com sucesso!'); location.href='login.php' </script>";
}}}
else {
echo $mysqli->error;
}}
}}
?>

Thanks to anyone trying to help me!

    
asked by anonymous 03.04.2016 / 21:02

1 answer

3

You can solve this problem using the success callback of your AJAX request.

You can maintain your existing validation logic, but you must return a result to the AJAX function and invoke it whenever a button is clicked, or when the input changes.

var username = $('#username').val();
    $('#username').change(function() {
       $.ajax({
          url: "validacao.php",
          type: 'POST',
          data: 'username=' + username,
          success: function(result){
                     if(result > 0){
                         // faça alguma coisa se o usuário já exista
                     }
                     else{
                         // faça alguma coisa se o usuário ainda não exista
                     }
                   }
          });
    });

If you do not know AJAX well, you can take a look at in this link , explains in more detail how AJAX requests work with an example similar to what you need to do:)

    
04.04.2016 / 06:00