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 removed, since when reloading the page it tries to do the insert
.
It would also be worth doing this in some other way but it doesn't occur to me, since this form is in the one footer
of the page ( it footer
is in a separate file "template/footer.php")
What options do I have to do something like that?
I have changed the title of the question
There is a way called PRG (Post-Redirect-Get) Pattern , which prevents you from submitting forms twice.
That is, when the form is sent via POST you have to REDIRECT using the header() function , to a page for example
enviado.php
with a context similar to:Formulario enviado con exito
, this causes you to be redirected to the pageenviado.php
with anHEADER
HTTP 3xx and then answer the served with a GET HTTP 200 and it serves you the pageenviado.php
, and thus avoids that the form is not sent again throughPOST
another time.I leave you a more descriptive image:
Image source: Wikipedia - Post/Redirect/Get
Your code would then be as follows and remember that you have to put this block of code before your code
HTML
, to avoid Header already sent... errors :You can use the function of
php
unset
.unset()
destroys the specified variables.The behavior of
unset()
inside a function can vary depending on what type of variable you are trying to destroy.If a global variable is
unset()
inside a function, only the local variable is destroyed. The variable in the environment of the call will keep the same value before the call tounset()
.Here is a link to the manual: http://php.net/manual/es/function.unset.php
Example:
EDIT
Try adding this:
Then in 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 need to do is redirect the user after saving the changes.
Imagine that the current situation is:
What needs to be changed is that contact.php, after saving the form data, should redirect to another page (for example to the front page) instead of displaying the result on the same page. This will no longer show that message confirming the resubmission of the form. If you're programming PHP bareback, redirection is as easy as doing
header('Location: '.$url);
I got it from here: http://librosweb.es/foro/pregunta/983/como-hago-para-evitar-el-mensaje-confirmar-reenvio-del-formulario/
I have controlled it by doing something like the following, although it only serves to skip reprocessing the last sent when refreshing the page with the F5 key, but it suits me well when what I send disappears from the pending list to be processed:
Answering 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 @aldanux and @alberto- say. Wed in EDIT 2. For example:
form.php : Contains the html and in the tag
<form>
add the attributeaction="enviar.php"
send.php : Contains the php that inserts the data into the database:
It is not necessary to use unset to delete the
$_POST
, after saving the data it redirects you to the form again with value by url called result which you get with a$_GET
so that you can control the result message if you wish.Use unset , if you set it equal to another value you will not destroy it, it will simply have another value, be it undefined or null, but it will be defined.
much simpler still... i solved it this way: 1. in the method of the form i use GET and 2. by ajax i normally send the form data via POST and i forgot about the topic... thanks for your help... everything worked for me
It worked for me by inserting this script where I updated.