How to refresh the page without forwarding information to the database in php

1

I'm working on a simple messaging system, and it's up until it works, however whenever a refresh is given on the page the information from the last insert is forwarded to the database. And if nothing has been filled, and given refresh it simply sends an empty value to the database, just as if you leave the page and go back to it the empty values are sent in the same way. I searched a lot in google and tested a lot of things, but nothing worked so far, I found some that tried to undo replicated data, however in that case I would like it to be sent duplicate anyway if it is done by submit by the user. Anyone know if you can differentiate submit from refresh?

I put an image showing the page and the error message that appears when given F5, this is not unique to firefox. Blank messages with a blank name are what I said earlier.

Follow the code:

                               Danilo Macedo Bakun                                                                      

  • Home
  •                     
  • Curriculum
  •                     
  • Contact
  •                     
  • Comments
  •                                       
        <div id="tudo">
            <form id="formulario" action="comentarios.php" method="POST">
                <h1>Nome:</h1>
                <input type="text" id="nome" name="Nome" required><br>
                <h1>Mensagem: </h1><br>
                <textarea  id="mensagem" name="Mensagem" required></textarea><br>
                <input type="submit" name="enviar" value="Enviar">
            </form>
    
            <?php
                $strcon = mysqli_connect('localhost','root','','teste') or die('Erro ao conectar ao banco de dados');
    
                if (isset($_POST['Nome']) === true) 
                {
                    $nome = $_POST['Nome'];
                } else {
                    $nome = false;
                }
    
                if (isset($_POST['Mensagem']) === true) 
                {
                    $mensagem = $_POST['Mensagem'];
                } else {
                    $mensagem = false;
                }
    
                $sql = "INSERT INTO mensagem VALUES ('$nome', '$mensagem')";
                mysqli_query($strcon,$sql) or die("Erro ao tentar cadastrar registro");
    
    
                $consulta = "SELECT * FROM mensagem";
                $sqlc = mysqli_query($strcon,$consulta) or die("Erro ao tentar consultar registro");
                while($aux = mysqli_fetch_assoc($sqlc)) 
                { 
                    echo "<h1>-----------------------------------------</h1><br>"; 
                    echo "<h1>Nome: ".$aux["nome"]."</h1><br>";
                    echo "<h1>Mensagem:".$aux["mensagem"]."</h1><br>";
                }
    
                mysqli_close($strcon);
            ?>
        </div>
    </body>
    

        
    asked by anonymous 29.06.2018 / 21:15

    2 answers

    1

    You can do it in some ways:

    • Make the form post by ajax and clear the fields (by javascript). This way, when the user presses F5, it will only reload the page instead of resending the data.
    • Post the form by submitting the FORM to another PHP page. When the other PHP page finishes entering the database, you can redirect it to the page that has your form.
      

    Here is the code suggestion for the second option:

    your_formula_html.php

    <div id="tudo">
            <form id="formulario" action="trata_comentarios.php" method="POST">
                <h1>Nome:</h1>
                <input type="text" id="nome" name="Nome" required><br>
                <h1>Mensagem: </h1><br>
                <textarea  id="mensagem" name="Mensagem" required></textarea><br>
                <input type="submit" name="enviar" value="Enviar">
            </form>
        </div>
    </body>
    

    trata_comentarios.php

    <?php
                $strcon = mysqli_connect('localhost','root','','teste') or die('Erro ao conectar ao banco de dados');
    
                if (isset($_POST['Nome']) === true) 
                {
                    $nome = $_POST['Nome'];
                } else {
                    $nome = false;
                }
    
                if (isset($_POST['Mensagem']) === true) 
                {
                    $mensagem = $_POST['Mensagem'];
                } else {
                    $mensagem = false;
                }
    
                $sql = "INSERT INTO mensagem VALUES ('$nome', '$mensagem')";
                mysqli_query($strcon,$sql) or die("Erro ao tentar cadastrar registro");
    
                header("Location:/seu_formulario_html.php");
                exit;
            ?>
    

    If you notice, I deleted that SELECT that you did. To list the messages, you can put this SELECT in the same PHP as your form.

        
    29.06.2018 / 21:28
    1

    The easiest way is to separate your HTML from your PHP, just create a page for example get.php and put the form action pointing to that page, when save redirects to the form page.

        
    29.06.2018 / 22:14