Send PHP message on the same HTML page

0

I'm developing a registration page and I'm validating it with PHP. I would like to notify the user on the same registration page when there are any errors.

Below is the form code (it has a session.start() in the file):

<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>
    
asked by anonymous 08.12.2018 / 20:46

1 answer

0

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 the two are used since front- end can be disabled by the user, with php what you can do, create a session as soon as the user accesses the page, follow the example

form.php

<?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>

cadastro.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{
    #...
}
?>
    
08.12.2018 / 22:50