Re: Emtying Variables
| From: | PHP | Date: | Sat, 17 Jul 2004 18:35:48 +0000 |
| Subject: | Re: Emtying Variables | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-190860@lists.php.net to get a copy of this message | ||
"Harlequin" <michael.mason@arraspeople.co.uk> wrote in message
news:20040717133440.14589.qmail@pb1.pair.com...
> What's the best way of emptying my variables once my form has done with
them
> because although I disconnect from the database if the user refreshes the
> screen it sends another e-mail to me.
>
> --
> -----------------------------
> Michael Mason
> Arras People
> www.arraspeople.co.uk
> -----------------------------
Hello,
I don't know if there is really a best way to do this but, some good ways:
- The first step is to avoid multiple submits with the same form when the
user submits severals times. You have to add a small script to avoid this:
<script language="JavaScript">
var SubmitCount = 0;
function CheckSubmit() {
if (SubmitCount == 0) {
SubmitCount++;
pPost.submit();
}
else {
// Anti stress alert ;-))
alert("The form has been sent. Please wait.... take time to drink a
coffee.");
return false;
}
}
</script>
<form action="myscript.php" method="post" name="myscript">
<input type=... your form here...
<input name="submit" type="button" value="Sumit"
onClick="CheckSubmit();">
</form>
- The second step is to avoid the refresh process:
You can use a redirect: header('location: ' . $_SERVER['PHP_SELF']); exit;
And you can also used a php variable to detect multiple submit:
The client side code becomes:
-- mypage.html
<script language="JavaScript">
var SubmitCount = 0;
function CheckSubmit() {
if (SubmitCount == 0) {
SubmitCount++;
pPost.submit();
}
else {
// Anti stress alert ;-))
alert("The form has been sent. Please wait....");
return false;
}
}
</script>
<form action="myscript.php" method="post" name="myscript">
<input type="hidden" value="[[CONTROL]]" name="control">
...
<input type=... your form here...>
...
<input name="submit" type="button" value="Sumit"
onClick="CheckSubmit();">
</form>
-- myscript.php
// When you display the page with the form
// Load the form page
$template =& new CTemplate("mypage.html");
// Put the control value
$template->assign("[[CONTROL]]", "" . time() . mt_rand());
...
// When you get the form data
// Two cases:
if ($_SESSION['sav_control'] != $_POST['control']) {
// Really the first time: the sav_control variable has not been set
// your code to process the form data
// Keep the control variable
$_SESSION['sav_control'] = $_POST['control'];
// Display the next page
...
}
else {
// Not the first time: the sav_control variable has been set
// Display the next page
...
}
So, if the user uses the back button, you keep in a session that this form
has already been sent.
Sorry if I use a template style... I think it is a better way to have a good
split between HTML and PHP and it's always easier when you want to explain
something ;-)
Hope this helps... Regards
Patrick