| 10:36 am on May 13, 2010 (gmt 0)|
Hi there Marked,
<form method="POST" action="">
<input type="submit" value="Submit" name="submit"/>
if(isset($_POST['submit']) && ($_POST['submit'] == "Submit"))
execute some code when the form is submitted
This is what I use, and I haven't had a 'double submit' yet. Ie check the value of the key, then process.
Changing $_POST to $_GET is only good if you create the vars and pass them in the URL, $_POST is from the form submitted data.
I suppose there is some fancy js out there that would disable the submit button when it is actioned, but I have never tried this method.
Hope this helps..
| 10:50 am on May 13, 2010 (gmt 0)|
another easy way is just to put a short-lived cookie on their system when they submit (just a couple of minutes will probably do)
if you check for the existance of that cookie everytime someone new submits, then you will know whether to throw an error.
| 11:23 am on May 13, 2010 (gmt 0)|
You can redirect the user to other page after submitting the form once. Here is the code you can use in mail configuration:
This can be used to avoid second hits.
| 4:50 pm on May 13, 2010 (gmt 0)|
scripts shouldn't post to themselves
post to a processing script that has no output
on error reinclude the form
on success redirect to a success page
| 7:51 am on May 14, 2010 (gmt 0)|
Hi there jatar_k,
I should have been clear about that really, posting to a dedicated php file or class enables you to handle things better, but you can still do error checking and blank submissions from posting to 'itself'.
I think though setting in place a dedicated form handler is just better practise, from there you can handle all exceptions. Just my opinion there, there may be better options that I haven't encountered yet ;)
| 6:37 pm on May 14, 2010 (gmt 0)|
What I've been using recently is a jquery pop up window that uses ajax to post the form to a processing page and then returns either "success" or an error message (or messages). If success a notification of the fact pops up and then the jquery window gets automatically closed half a second later. If error message(s) then notification of what went wrong occurs.
Any reason this way of doing things would be a horrible idea?
| 9:57 am on May 15, 2010 (gmt 0)|
Thanks for all your replies :)
Ok, what I did was create this function:
It works pretty well. However does not work when the user tries to go back. At the moment though I'm pretty satsified with this system. :)
|brotherhood of LAN|
| 10:02 am on May 15, 2010 (gmt 0)|
It would be recommended to also validate the form server-side as well as client-side.
Client-side validation saves the hassle of page reloads, but ultimately server-side validation ensures that the data is truly valid.
| 10:37 am on May 15, 2010 (gmt 0)|
Hi there marked,
To add onto brotherhoodoflans point, not all people have js enabled, so for user/cross platform compatibility it would be preferable to use the php validation as its server side and not client side.
| 4:57 pm on May 15, 2010 (gmt 0)|
|not all people have js enabled |
Two cents on that . . . in cases of user input, it's not so much your users (which is important on it's own) but that those who would abuse your site do so from command line apps without even touching the form. They completely circumnavigate the form with a post directly to your script. In such cases they can inject data you wouldn't expect.
| 5:07 pm on May 15, 2010 (gmt 0)|
Hi there Marked,
if(isset($_POST['hidden_field']) && ($_POST['hidden_field'] == "hidden_field"))
Ok, so your checking the key, at least check the value of the key to see if it is what it should be, after all it could be set, but with a completely different value from what you assigned to it, kinda like rocknbil suggests :)
I find that checking both value and key is better as you can then direct the script accordingly, and as jatar_k says, keep the files seperate, or direct to a dedicated file so that you can then show the user something else after they have posted, or redirect back to the same page if an error arises ie: blank field; illegal data etc, etc.
Hope this helps ;)
| 6:39 pm on May 15, 2010 (gmt 0)|
&& ($_POST['hidden_field'] == "hidden_field")
I'm pretty sure that a string "is equal to" 0 - so the "is identical to" comparison should be used here, to prevent users doing any damage during an injection attempt, or exposing any vulnerabilities:
&& ($_POST['hidden_field'] [b]===[/b] "hidden_field")
Just to be safe :)
| 6:58 am on May 18, 2010 (gmt 0)|
I generally tend to use a random key to stop multiple form submission. For eaxmple:
// Process form
if(isset($_GET['submit']) && $_GET['key'] == $_SESSION['key'])
echo 'not prcoessed';
$_SESSION['key'] = mt_rand(1, 1000);
<!-- Form -->
<form action="" method="get">
<input type="hidden" name="key" value="<?php echo $_SESSION['key'] ?>" />
<input type="submit" name="submit" value="Submit" />
Hope this helps!