Display error message just below the form with the action on the same page!

0

It's this ... I'm creating a kind of quiz where the player has to deduce the answer and put it in the input. Correct case is redirected to the next level, if it is wrong it remains on the page and a message is displayed saying that it is not correct. At least that's the idea kkk. The problem is that the message that is to appear only when the player misses is appearing while loading the page, without even having put the answer in the input, understand? How can I fix this? !!! Oh yeah ... It's my first time posting a question, forgive me for lack of objectivity or the like.

                                                                 NoCase|Level1        

<?phpinclude("includes/fra.php"); ?>
 <div id="wrapper">

 <!-- CAPTURANDO A RESPOSTA -->
 <?php $resposta = isset($_POST["massive"])?$_POST["massive"]:"none";?>

            <div class="text-center">
                <p style="font-size: 22pt;">1¹</p>
                <form action="#wrapper" id="mass" method="POST">
             <input type="text" class="form-control" autocomplete="off" name="massive" id="massive" placeholder="RESPOSTA">
                </form>

                 <!-- TÁ PRINTANDO AO CARREGAR A PÁGINA E NÃO QUANDO O USUÁRIO ERRA-->   
                <?php $resposta != 1?print $frases[$rand_keys]: header("Location:example.com");?>


                <button form="mass" type="submit" class="damn_botao"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></button>

            </div>
 </div>

    
asked by anonymous 06.07.2018 / 10:18

1 answer

0

When the page loads $resposta will not be 1, it will be none for the logic you have at the beginning of the file, so it always prints the error message.

I suggested that you check and redirect at start up, because the PHP header() command when used to redirect expects that there is still no output on the page, which in your case has some HTML, a message will probably appear error, although it is still possible to redirect and not see. Ideally avoid this, even to be not being logged in the error log.

<?php include("includes/fra.php"); ?>
<?php
$errou = false;
// o form foi submetido, então fazemos a verificação se acertou ou não
if (isset($_POST["massive"])) {
    // assumimos que falhou, porque fazemos redirect caso esteja certo e então nunca vamos imprimir a mensagem, mas se não 
    // fosse para fazer redirect, metiamos este $errou = true dentro do else do if para apenas o mudar se falhasse
    $errou = true;
    if ($_POST["massive"] == 1) {
        // acertou!
        header("location:example.php");
    }
}
?>
<div id="wrapper">

// etc..

<!-- TÁ PRINTANDO AO CARREGAR A PÁGINA E NÃO QUANDO O USUÁRIO ERRA-->   
<?php ($errou) ? print $frases[$rand_keys] : ""; ?>
    
06.07.2018 / 11:15