Verifies Registered User

Asked

Viewed 53 times

0

I’m using this topic as an example of implementation.

/a/76694

It works, gives me the message that there is already a registered email, BUT if I click register it registers anyway, ignoring the message.

I need a way to force the user to type another email again

checkEmail.html

<script language="javascript">
var email = $("#email"); 
    email.blur(function() { 
        $.ajax({ 
            url: 'verificaEmail.php', 
            type: 'POST', 
            data:{"email" : email.val()}, 
            success: function(data) { 
            console.log(data); 
            data = $.parseJSON(data); 
            $("#resposta").text(data.email);
        } 
    }); 
}); 
</script>

verificaEmail.php

<?php
if(isset($_POST['email'])){ 

#Recebe o Email Postado
$emailPostado = $_POST['email'];

$con = mysqli_connect("localhost", "root", "", "outrasintencoes");
$sql = mysqli_query($con, "SELECT * FROM usuarios WHERE email = '{$emailPostado}'") or print mysql_error();

if(mysqli_num_rows($sql)>0) 
    echo json_encode(array('email' => 'Ja existe um usuario cadastrado com este email')); 
else 
    echo json_encode(array('email' => 'Usuário valido.' )); 
}
?>
  • Tip! It makes it much easier to answer, if you put the reference link just to situate your question, but you also need to put your code to know where the error is

1 answer

0

As I would:

PHP

#Recebe o Email Postado
$emailPostado = $_POST['email'];

$con = mysqli_connect("localhost", "root", "", "outrasintencoes");
$sql = mysqli_query($con, "SELECT * FROM usuarios WHERE email = '{$emailPostado}'") or print mysql_error();

if(mysqli_num_rows($sql)>0) 
    echo json_encode(array('existe' => 'S', 'email' => '[email protected]')); 
else 
    echo json_encode(array('existe' => 'N')); 
}
?>

JS

<script language="javascript">
var email = $("#email"); 
    email.blur(function() { 
        $.ajax({ 
            url: 'verificaEmail.php', 
            type: 'POST', 
            data:{"email" : email.val()}, 
            success: function(data) { 
            console.log(data); 
            data = $.parseJSON(data); 
            if(data.existe == 'S'){
             //Beleza dx ele fazer o cadastro
            }else{
             //Deixa o campo vermelho, disabilita o submit, avisa que email já está sendo usado
            }
        } 
    }); 
}); 
</script>

  • I don’t know if it will work the way it is, it may be missing a comma or something like that. pq n had to debug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.