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

Asked

Viewed 2,153 times

1

I am working on a simple message system, and it works, but whenever a refresh is given on the page, the information of the last Internet is sent back to the database. And if nothing has been filled, and it has been given refresh it simply sends an empty value to the database, as if leaving the page and going back to it are sent the empty values in the same way. I researched a lot on google and tested several things, but nothing worked so far, I found some that tried to undo replicated data, but in this case I would like to be sent duplicate anyway if it is done by user Ubmit. Does anyone know if they can tell the difference between Ubmit and Refresh?

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

Follows the code:

Danilo Macedo Bakun

  • Home
  • Resume
  • 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>
    

    inserir a descrição da imagem aqui

    2 answers

    1


    You can do it in some ways:

    • Post the form 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 in the form by submitting the FORM to another PHP page. When the other PHP page is finished inserting into the database, you can redirect to the page that has your form.

    Follow code suggestion for the second option:

    seu_formulario_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 you made. To list messages, you can put this SELECT in the same PHP as your form.

    • I managed to resolve using another php page, thank you very much for the help.

    1

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

    • I managed to resolve using another php page, thank you very much for the help.

    Browser other questions tagged

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