Send PHP message on the same HTML page

Asked

Viewed 673 times

0

I am developing a registration page and I am validating it with PHP. I would like to notify the user on the same registration page when there is an error.

Below is the form code (you have a session.start() in the archive):

<form name="form" method="post" action="validacao.php">
    <center>
        <input type="text" name="nome" value="" maxlength="9" class="estilo_form" placeholder="Nome"/>
        <input type="text" name="usuario" value="" maxlength="9" class="estilo_form" placeholder="Usuário"/>     
        <input type="password" name="senha" value="" maxlength="9" class="estilo_form" placeholder="Senha"/>
        <input type="password" name="senhaagain" value="" maxlength="9" class="estilo_form" placeholder="Digite novamente a Senha"/>
    </center>
    <br/>
    <input type="checkbox" name="concordar" value="S" class="input_check"/>
    <font style="float:left;"/> Li e Concordo com os <a href="" class="estilo_link">Termos de uso</a>. </font>
    <a href="inicio.html"> 
        <input type="submit" name="submit" value="Enviar" class="botao2">
    </a>
</form>

PHP file:

<html>
    <?php session_start(); ?>
        <body>
            <?php 
            $all = $_POST;
            $nome = $_POST['nome'];
            $usuario = $_POST['usuario'];
            $senha = $_POST['senha'];
            $senha_again = $_POST['senhaagain'];
            $_SESSION['validador'] = ;

            if ( !isset( $all ) || empty( $all ) ) {
            $_SESSION['validador'] = 1; //Validador para por um echo na pagina de cadastro, juntamente com um if.
            }
            ?>
        </body>
</html>
  • I believe it is just make an IF in the place you want: <? if(condição){ echo "algo deu errado"; ?>.

  • I’ve done what I wanted, thank you guys.

1 answer

1

As far as I know you can use two types of validation, the back-end, in your case with PHP, and the front-end (With Javascript), usually use both since the front-end validations can be disabled by the user, create a session as soon as the user accesses the page, follow example

php form.

<?php
session_start();
$mensagem = $_SESSION['mensagem'];

if ($mensagem != "") {
    echo $mensagem;
}

?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form action="cadastro.php" method="POST">
    <input type="text" name="nome">
    <input type="submit" value="Enviar">
</form>
</body>
</html>

php.

<?php
session_start();
$nome = $_POST['nome'];

if(empty($_POST['nome'])){
    $_SESSION['mensagem'] = "O nome não pode ficar vazio";
    header("location: formulario.php");
}else{
    #...
}
?>
  • I understood friend. However, the program is circumventing my conditions and the message is appearing even before sending the form. Would have something to do with unset()? I have tried by isset(_POST['Submit']) in condition and did not help anything. Thanks from now on.

Browser other questions tagged

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