problem with form duplicity

Asked

Viewed 94 times

0

I’m having a very common problem, and I don’t know how to solve it. I’m going to graze a very simple php page to explain.

The problem occurs when the user submits the form, and when he clicks on the back feature of the browser, the form is submitted again, generating a duplicity in the BD. You have some way of handling it?

Follow the code on my form:

<?php
//======================================================================================================================
// Envia formulário
//======================================================================================================================
if ((!empty($action)) and ($action === "add")) {

    // Recebe dados do formulario
    $nome = addslashes(filter_input(INPUT_POST, 'nome', FILTER_SANITIZE_SPECIAL_CHARS));
    $idade = addslashes(filter_input(INPUT_POST, 'idade', FILTER_SANITIZE_NUMBER_INT));

    // Verifica se informou os dados
    if ((empty($nome)) || (empty($idade))) {
        ?>
        <script>
            alert('ERRO. Iforme todos os dados'); 
            history.back();
        </script>
        <?php
        die;
    }

    // Registra no BD
    $inserir1 = Query($mysqli, "INSERT INTO cadastro VALUES ('0', '$nome', '$idade')");

    // Verifica se cadastrou
    if ($inserir1) {
        ?>
        <script>
            alert('ERRO. Iforme todos os dados'); 
            window.location='cadastro.php';
        </script>
        <?php
    } else {
        ?>
        <script>
            alert('ERRO. Tente mais tarde'); 
            history.back();
        </script>
        <?php
    }
}
//======================================================================================================================
?>


<form name='form'  onsubmit='return validacao()' method=post action='cadastro.php?action=add' >

    <input  type="text" name="nome">
    <input  type="text" name="idade">

    <input type="submit" name="finalizar">
</form>
  • when you save the data in the database, you can make a reset complete, redirecting with the function header followed by a exit. And you should also prepare the database so that it does not register replicas.

  • When you check if you have registered in if Else only returns Alert with error? Can this Arnaldo?

2 answers

2


That way when you click the return arrow from the browser Session will prevent you from resending the form and submitting it again.

 <?php
 session_start();
 ...............
 ...............
  if ($_SESSION["nome"]==""){
   $inserir1 = Query($mysqli, "INSERT INTO cadastro VALUES ('0', '$nome', '$idade')");
   $_SESSION["nome"] = $_POST["nome"];
  }                

1

A common way to solve this problem is to redirect the user to their own page to clear the post data:

//antes de qualquer saída (echo, caracteres etc)
//ao invés do window.location
header('Location:cadastro.php');

In your case you should also check if the request is POST or GET before processing the result:

if (!empty($_POST) && !empty($action) && $action === "add") {
    ...
  • That way it would be ideal, the problem and that before the header('Location:cadastro.php'); I already run some echo.

  • sa second form (!empty($_POST) does not solve much, because if the user returns the browser sends the form via POST.

Browser other questions tagged

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