3
I wanted to do the password check, if the passwords are different an Alert appeared with something like "Passwords do not match", if yes, pass through. But the way I’m doing it comes a message from the $message "Passwords are equal", but it’s still sending the data to the bank.
<?php
$username = 'root';
$password = '';
$connection = new PDO( 'mysql:host=localhost;dbname=nise', $username );
$query = "INSERT INTO usuario (nome, sobrenome, matricula, email, senha)
VALUES (:nome, :sobrenome, :matricula, :email, :senha)";
$statement = $connection->prepare($query);
$valores = array();
$valores[':nome'] = (isset($_POST['primeiroNome']) ? $_POST['primeiroNome'] : '');
$valores[':sobrenome'] = (isset($_POST['sobrenome']) ? $_POST['sobrenome'] : '');
$valores[':matricula'] = (isset($_POST['matricula']) ? $_POST['matricula'] : '');
$valores[':email'] = (isset($_POST['email']) ? $_POST['email'] : '');
$valores[':senha'] = (isset($_POST['senha']) ? $_POST['senha'] : '');
$result = $statement->execute($valores);
if($_POST) {
$senha = $_POST['senha'];
$confirma_senha = $_POST['confirma_senha'];
if ($senha == "") {
$mensagem = "<span class='aviso'><b>Aviso</b>: Senha não foi alterada!</span>";
} else if ($senha == $confirma_senha) {
$mensagem = "<span class='sucesso'><b>Sucesso</b>: As senhas são iguais: ".$senha."</span>";
} else {
$mensagem = "<span class='erro'><b>Erro</b>: As senhas não conferem!:".$confirma_senha."</span>";
}
echo "<p id='mensagem'>".$mensagem."</p>";
}
?>
This part of the script warns if the passwords match, if yes it does not from any Alert, if not, a DIFFERENT PASSWORDS appears, but when I press "OK" to close the Alert the form data is removed and sent to the database anyway, and this was not to happen
<script> function validarSenha(){
senha = document.formulario.senha.value
confirma_senha = document.formulario.confirma_senha.value
if (senha == confirma_senha) alert
else alert("SENHAS DIFERENTES")
}
</script>
And here’s my form
<form method="POST" action="registro.php" name="formulario">
<div class="form-group">
<div class="form-row">
<div class="col-md-6">
<label for="primeiroNome">Primeiro nome</label>
<input type="text" class="form-control" id="primeiroNome" name="primeiroNome" placeholder="Digite seu primeiro nome" required="required">
</div>
<div class="col-md-6">
<label for="Sobrenome">Sobrenome</label>
<input type="text" class="form-control" id="Sobrenome" name="sobrenome" placeholder="Digite seu Sobrenome" required="required" autofocus="autofocus">
</div>
</div>
</div>
<div class="col-md-6" id="matricula">
<label for="primeiroNome">Matrícula</label>
<input type="text" class="form-control" name="matricula" placeholder="Digite sua matrícula" required="required" autofocus="autofocus">
</div>
<div class="form-group">
<label for="email">E-mail</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Digite seu email" required="required" autofocus="autofocus">
</div>
<div class="form-group">
<div class="form-row">
<div class="col-md-6">
<label for="senha">Senha</label>
<input type="password" class="form-control" id="senha" name="senha" placeholder="Digite sua senha" required="required" autofocus="autofocus">
</div>
<div class="col-md-6">
<label for="confirma_senha">confirmar senha</label>
<input type="password" class="form-control" id="confirmaSenha" name="confirma_senha" placeholder="Confirme sua senha" required="required" autofocus="autofocus">
</div>
</div>
</div>
<div>
<input type="submit" onClick="validarSenha()" class="btn btn-primary btn-block" value="Registra-se"/>
</div>
<div class="text-center">
<a href="#" class="d-block small mt-3">Esqueceu sua senha?</a>
<a href="login.php" class="d-block small mt-3">Login?</a>
</div>
</form>
Note:If anyone has suggestions of more beautiful Lert can send...
I made some changes to my answer. Test everything and comment if something goes wrong or if there is another question about the answer.
– adrianosmateus
Manoo turned out all right, I was only getting back the keys of the script, Valew same guy already 6 hour I was trying to do this, but always registered in the bank even with the wrong password. '----------'
– Paulo Victor
How great that it worked. If more problems arise open new questions. Success!
– adrianosmateus
Could you help me with this question https://answall.com/questions/334660/queria-colocar-um-alert-na-minha-tela-de-login-php-js-html-mysql/334665#334665
– Paulo Victor