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

Asked

Viewed 347 times

0

Here’s the thing... I’m creating a kind of quiz where the player has to deduct the answer and put it into the input. If it is correct it is redirected to the next level, if it is wrong it is still 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 it?!!! Ah, yes... It’s my first time posting a question, forgive lack of objectivity or the like. aaaaaaaaa

Nocase | Level 1

    <?php include("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>

1 answer

0


When the page loads $resposta it won’t be 1, it’ll be none by the logic you have at the beginning of the file, hence it always print the error message.

I suggested that you put the check and redirect at the beginning, even because the command header() PHP when used to redirect, expect that there is still no output on the page, that in your case you have some HTML, probably will appear an error message, although it is still possible to redirect and not see. Ideally you avoid this, not even to be logged in the bug 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] : ""; ?>

Browser other questions tagged

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