How to avoid resubmitting the form?

5

Having this:

<footer>
  <div class="row no-margin no-padding">
    <div class="col-md-4  col-xs-6 oneplayer">
    <br>
      <a class="logo navbar-brand no-margin no-padding" href="index.php"><img class="logo-img" src="res/images/oneplayer_logo.png"></img></a>
    </div>
    <div class="col-md-8 col-xs-12">
      <div class="row no-margin no-padding">
        <div class="col-md-4 footer-links-container">
          <a href="aboutus.php" class="header-links"><?php echo $lang['footer_string_1']; ?></a><br><br>
          <a href="index.php#contact" class="header-links"><?php echo $lang['footer_string_4']; ?></a>
        </div>
        <div class="col-md-4 footer-links-container">
          <a href="politica_privacidad.php" class="header-links"><?php echo $lang['footer_string_2']; ?></a><br><br>
          <a href="condiciones.php" class="header-links"><?php echo $lang['footer_string_3']; ?></a>
        </div>
        <div class="col-md-4 footer-links-container">
          <form action="" method="post">
            <p class="footer-contact-info"><?php echo $lang['footer_string_5']; ?></p>
            <input type="email" class="my-form-control" placeholder="<?php echo $lang['footer_string_6']; ?>" name="newsemail" /> <button class="newsletter-button"><?php echo $lang['footer_string_7'];?></button><br> <br>
          </form>
          <?php
            if (isset ($_POST['newsemail'])){
              include("db_files/db.php");
              $newsemail = mysqli_real_escape_string($db, $_POST['newsemail']);
              $strSQL = "INSERT INTO newsletter ('email') VALUES ( '$newsemail')";
              $query = mysqli_query($db, $strSQL);
              unset($_POST['newsemail']);
            }
          ?>
          <p class="footer-contact-info"> 999 999 999
        </div>
      </div>
    </div>
  </div>
</footer>

I want that after doing the insert , the variable $_POST['newsemail'] is eliminated, since when reloading the page it tries again to do the insert .

It would also be worth doing this in some other way but I can not think of it, since this form is in the footer of the page (the footer is in a separate file "template / footer.php")

What options do I have to do something like that?

this is what I want to avoid:

I changed the title of the question

    
asked by Pavlo B. 11.11.2016 в 12:49
source

5 answers

6

There is a form called PRG (Post-Redirect-Get) Pattern , which prevents sending forms in duplicate.

That is, when the form is sent using POST you have to REDIRECT using the header () , to a page for example enviado.php with a context similar to: Formulario enviado con exito , this causes you to redirect you to the page enviado.php with a HEADER HTTP 3xx and then answer the server with a GET HTTP 200 and the page enviado.php serves you, and thus you avoid sending the form again by POST again.

I leave you a more descriptive image:

Image source: Wikipedia - Post / Redirect / Get

Your code would then be like this and remember that you have to put this block of code before your code HTML , to avoid errors Header already sent ... :

<?php
if (isset ($_POST['newsemail'])){
    include("db_files/db.php");
    $newsemail = mysqli_real_escape_string($db, $_POST['newsemail']);
    $strSQL = "INSERT INTO newsletter ('email') VALUES ( '$newsemail')";
    $query = mysqli_query($db, $strSQL);
    unset($_POST['newsemail']);

    // Redirecciona a la página que deseas
    header('Location: 'enviado.php');
}
?>

// Y aqui el resto de tu código HTML
    
answered by 11.11.2016 в 14:06
3

You can use the php unset function.

unset() destroys the specified variables.

The behavior of unset() within a function can vary depending on what type of variable you are trying to destroy.

If a global variable is unset() within a function, only the local variable is destroyed. The variable in the call environment will maintain the same value before calling unset() .

You leave a link to the manual: link

Example:

$foo = 'bar';
unset($foo);

EDIT

Try adding this:

<script>
    var cuenta=0;
    function enviado() {
        if (cuenta == 0) {
            cuenta++;
            return true;
        } else {
            return false;
        }
    }
</script>

Then on the label form you have to add the following: onSubmit="return enviado()"

EDIT 2

The solution in this case is simple but requires a small change in the function that saves the form data. To avoid that message, what you should do is redirect the user after saving the changes.

Imagine that the current situation is:

El usuario entra en /contacto.php, rellena el formulario y le da a Enviar.
El script contacto.php procesa los datos del formulario. Si hay errores, vuelve a mostrar la página con un mensaje. Si todo está bien, guarda los datos y muestra un mensaje de tipo "Hemos recibido tu mensaje. Gracias por contactarnos. Te responderemos pronto".

What needs to change is that contacto.php, after saving the form data should redirect to another page (eg the cover) rather than show on the same page. This message will no longer be shown confirming the re-submission of the form. If you are programming PHP "bareback", the redirect is as easy as doing header('Location: '.$url);

What I got from here: link

    
answered by 11.11.2016 в 12:54
1

I have controlled it by doing something like the following although it only serves to omit reprocessing the last sent when refreshing page with F5 key, but it fits me well when what I send disappears from the pending list to be processed:

<?php
session_start();

if( !empty($_GET['txt'])  &&  !( isset($_SESSION['GET_PREVIO']) && !empty($_GET['txt']) && $_SESSION['GET_PREVIO'] == $_GET['txt'] )   )
{
    $_SESSION['GET_PREVIO'] = $_GET['txt'];
    echo "se recibio {$_GET['txt']}";
    //echo "  <script> window.location.href='ddd.php'; </script>";
}
?>
<html>
    <head>
        <meta http-equiv='refresh' CONTENT='10'>
    </head>

    <body>
        <form method='GET' action='ddd.php'>
            <input type='text' name='txt' >
            <input type='submit' name='btn' value='enviar' >
        </form> 
    </body>
</html>
    
answered by 02.05.2018 в 23:42
0

Use unset , if you equal it to another value you will not destroy it, it will simply have another value be undefined or null, but it will be defined.

    
answered by 11.11.2016 в 12:53
0

Responding more briefly and clearly, a more common solution is that you have two files, one with the html of the form and another with the php that processes the data and redirects at the end of the process, more or less as it says @aldanux and @ alberto-mier in EDIT 2. For example:

form.php : Contains the html and in the tag <form> add the attribute action="enviar.php"

<form action="enviar.php" method="POST">
    <p class="footer-contact-info"><?php echo $lang['footer_string_5']; ?></p>
    <input type="email" class="my-form-control" placeholder="<?php echo $lang['footer_string_6']; ?>" name="newsemail" />
    <button class="newsletter-button"><?php echo $lang['footer_string_7'];?></button>
    <br>
</form>

enviar.php : Contains the php that inserts the data in the database:

<?php
$resultado = "";
if (isset ($_POST['newsemail'])){
    include("db_files/db.php");
    $newsemail = mysqli_real_escape_string($db, $_POST['newsemail']);
    $strSQL = "INSERT INTO newsletter ('email') VALUES ( '$newsemail')";
    $query = mysqli_query($db, $strSQL);
    if($query){
      $resultado = "exitoso";
    }else{
      $resultado = "error";
    }
}
header('Location: formulario.php?resultado='.$resultado);
?>

It is not necessary to use the unset to delete the $_POST , after recording the data redirects you to the form again with value per url called result which you get with a $_GET so that you control the result message if you wish .

    
answered by 11.11.2016 в 16:05