0
I have a registration form, but I need that when filling the email field, when the person passes to the next field, he search the database and return a message warning if this email is already registered in the database. According to some questions I found here and elsewhere, I arrived at this method:
Register.php - Here is the JS section and the INPUT section
<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>
<input id="email" name="email" type="text" value="" placeholder="Digite seu e-mail" required>
<div id="resposta"></div>
verificaEmail.php
<?php
#Verifica se tem um email para pesquisa
if(isset($_POST['email'])){
#Recebe o Email Postado
$emailPostado = $_POST['email'];
#Conecta banco de dados
$con = mysqli_connect("localhost", "root", "", "academia");
$sql = mysqli_query($con, "SELECT * FROM usuarios WHERE Email = '{$emailPostado}'") or print mysql_error();
#Se o retorno for maior do que zero, diz que já existe um.
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.' ));
}
?>
But it’s not working. Someone can help me solve this, as I don’t intend much of jQuery and PHP, I’m suffering a little in this. I do not know if it is necessary, but here is PHP to register that this in FORM ACTION.
<?php
/* substitua as variáveis abaixo pelas que se adequam ao seu caso */
$dbhost = 'localhost'; // endereco do servidor de banco de dados
$dbuser = 'root'; // login do banco de dados
$dbpass = ''; // senha
$dbname = 'academia'; // nome do banco de dados a ser usado
$conecta = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$seleciona = mysqli_select_db($conecta, $dbname);
$nome = $_POST ["nome"];
$sobrenome = $_POST ["sobrenome"];
$email = $_POST ["email"];
$senha = $_POST ["senha"];
$telefone = $_POST ["telefone"];
$ddd = $_POST ["ddd"];
$sqlinsert = "INSERT INTO usuarios (ID, Nome, Sobrenome, Email, Senha, Telefone, DDD) VALUES (DEFAULT, '$nome', '$sobrenome', '$email', '$senha', '$telefone', '$ddd')";
$inserenome = mysqli_query( $conecta, $sqlinsert );
// inicia a conexao ao servidor de banco de dados
if(! $conecta )
{
die("<br />Nao foi possivel conectar: " . mysql_error());
}
echo "<br />Conexao realizada!";
// seleciona o banco de dados no qual a tabela vai ser criada
if (! $seleciona)
{
die("<br />Nao foi possivel selecionar o banco de dados $dbname");
}
echo "<br />Selecionado o banco de dados $dbname";
// finalmente, cria a tabela
if(! $inserenome )
{
die("<br />Nao foi possivel inserir registro: " . mysql_error());
}
echo "<br />Um novo registro foi feito!";
// encerra a conexão
mysqli_close($conecta);
?>
Haa, and the email is UNIQUE in MYSQL, so when n had this JS and verifieEmail, when I tried to register an existing email, it simply n registered and displayed the "Unable to enter record:"
It may be because your script is running before the element
#email
have been inserted into GIFT. Try putting your script after the element#resposta
.– rdleal
Blz, it worked, now only that even I put some email that is already registered, he says it already exists and everything, except that he registers the same.
– dalton gonzalo Fuentes
Example, in my field repeat the password: <input name="password_confirms" type="password" value="" placeholder="Confirm your password" required oninput="validateSenha(this)"> I have this script that while not in the correct way, does not let send the form: Function validationNew (input){ if (input.value != Document.getElementById('txtSenha').value) { input.setCustomValidity('The passwords do not match!'); } Else { input.setCustomValidity('); } }
– dalton gonzalo Fuentes
It seems now you need to block sending when the field
#email
is invalid. You can usesetCustomValidity
in the field#email
, within the function ofsuccess
of your request. I suggest you open another question to solve your new problem, since the question Check if the completed email already exists in the database in "REAL TIME" was apparently resolved.– rdleal
Good evening. I ask you to scold me if I’m wrong. By your logic you check if the email is registered in the database as soon as the user leaves the field but as soon as the php of the form is triggered to an Insert command without any conditions before it. In this case you would have to check again if the email is already registered in the database if it is you return the user to the page and ask to check the email, otherwise use the select command to enter the information.
– Airon