1
I am trying to make my page form return an error and remain on the page if no field is filled in.
However, he is doing this even though all fields are filled out correctly ! Could you help me ?
The page of the form:
<body>
<div class="container container-twelve">
<div class="four columns offset-by-four">
<h1 class="titles">Cadastro</h1>
<?php if(isset($_SESSION["success"])) {?>
<p><?= $_SESSION["success"] ?></p>
<?php }?>
<?php unset($_SESSION["success"]); ?>
</div>
<div class="four columns offset-by-four" id ="login">
<?php if (isset( $_GET['erro'] )) {
echo '<div class="erro">';
echo htmlentities( $_GET['erro'] );
echo "</div>\n";
} ?>
<form action="cadastra_usuario.php" method="post">
<label for="nome">Nome</label>
<input type="text" name="nome" placeholder="Digite seu nome">
<label for="email">Email de usuário </label>
<input type="text" name="email" placeholder="Seu email para login">
<label for="senha">Senha</label>
<input type="password" name="senha" placeholder="Sua senha">
<label for="senha2">Repita sua senha</label>
<input type="password" name="senha2" placeholder="Repita sua senha">
<input type="submit" value="Cadastrar">
</form>
<p><a href="index.php"> << Voltar para o site</a></p>
<p><a href="login.php"> Já tenho um cadastro >> </a></p>
</div>
</div>
</body>
</html>
the PHP:
<?php
include('conecta.php');
include('functions.php');
$senha = $_POST['senha'];
$senha2 = $_POST['senha2'];
$nome = $_POST['nome'];
$email = $_POST['email'];
if($senha != $senha2){
$erro = urlencode( 'As senhas não conferem!' );
header("Location: cadastro.php?erro=$erro");
die();
}
$verifica = verificaCadastra($nome, $email, $senha);
if( $verifica == null){
$erro = urlencode( 'Por favor, preencha todos os campos!' );
header("Location: cadastro.php?erro=$erro");
die();
}
$_SESSION["success"] = "Usuário cadastrado com sucesso.";
header("Location: login.php");
?>
the Function:
function verificaCadastra($nome, $email, $senha){
if(empty($nome)){
$nome == null;
}
if(empty($email)){
$email == null;
}
if(empty($senha)){
$senha == null;
}
}
Thanks my friend ! It worked perfectly !
– Gabriel