bbFury,
It's much simpler intercept form submission with an onsubmit handler that tests to see if submission has already been attempted and returns true (allow submission) or false (suppress further submissions) accordingly.
You will need a "submitted" boolean flag, initialisd to false. With a little ingenuity, you can avoid using the global namespace.
onload = function() {
var submitted = false;//initialise flag to false;
document.myForm.onsubmit = function() {
if(!submitted) { return (submitted = true); }//allow first form submission
else { return false; }//suppress further form submissions
}
};
Then your test function will be something like:
and the corresponding form tag:
<form name="myForm" method="..." action="...">
untested - may need debugging
Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
At its simplest, the script boils down to:
onload = function() {
var submitted = false;
document.myForm.onsubmit = function() { return (!submitted) ? submitted = true : false; };
};
Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
Hi Tom,
The code goes on the same page as your form. Stick it in tags within the document's
If the document already has an onload function, then simply paste my two lines of code into it.Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
bbFury,
My method doesn't cater for the user refreshing the page. You would need to use either a cookie (client-side) or session variable (server-side) for that.
In either case, if there is any chance that another legal form submission in the same session, you need to decide on (and implement) "expiry rules". This aspect could be probelmatic, though maybe someone else has first hand experience and will ba able to advise.
I suggest that a javascript trap (as per my suggestion above) is used as the first line of defence. My code can be modified to provide an alert if a subsequent submission is attempted:
onload = function() {
var submitted = false;
document.myForm.onsubmit = function() {
if(!submitted){ return submitted = true; }
else{
alert('Form has been submitted. Please be patient');
return false;
}
};
};
(With slightly more effort you could display the message on the page instead)
For the next line of defence, the simplest thing is probably to implement a "flood interval" - ie. on successful form submission, have email.php set a session variable (server-side) containing the date/time at which the successful request was made. The page needs to test this variable against current time in order to suppress repeat resuests within a certain time limit (the flood interval, which you hard code in email.php).
The user will definately want feedback to inform that the server has responded (or not). For this, you should arrange to email.php to do one of the following:compose and serve a page containing an appropriate feedback message (as per conventional php pages)
compose and return an appropriate feedback message (not in a page) for display using AJAX/DHTML techniques.
For the latter, the request (form submission) would need to be made in the AJAX way, which is a little mind-blowing if you've not done it before but not too tricky in the overall scheme of things. There a plenty of examples on Daniweb and elsewhere to help.
Either way, I don't think you should need to redirect (ie serve a different page from the one that was requested).
Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
Oh yes, tere are errors in your statement, which is equivalent to:
onload = function() {
var submitted = false;
document.myForm.onsubmit = function() { return (!submitted) ? submitted = true : false; };
};
onload = function() {
var submitted = false;
document.myForm.onsubmit = function() { return (!submitted) ? submitted = true : false; };
};
showpromo();
showclass();
It should be:
onload = function() {
var submitted = false;
document.myForm.onsubmit = function() { return (!submitted) ? submitted = true : false; };
showpromo();
showclass();
};
(In Daniweb remember to click "Toggle plain text" before copying code to clipboard or you can get everything twice).Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372