Page 1 of 1
how to stop inserting value in db clicking on refresh button
Posted: Wed Aug 04, 2010 3:01 am
by manojsemwal1
hai ,
i am using same form like (<form name=frm action=' $_SERVER[PHP_SELF]'>) to insert data into db but when i refresh the page the value again insert into db.
so how to stop the value to insert into db.
Thanks
Re: how to stop inserting value in db clicking on refresh bu
Posted: Wed Aug 04, 2010 3:49 am
by dejvos
I think it is not possible via PHP. I do that by JavaScript.
I put on the end of form processing this:
Code: Select all
<?php
// A processing of a form
echo "<script type=\"text/javascript\">window.location.href = \"page.php\"</script>";
exit;
?>
Re: how to stop inserting value in db clicking on refresh bu
Posted: Wed Aug 04, 2010 5:59 am
by manojsemwal1
thanks for you quick reply can we put some timer so that after some time it will be execute after give successful message.
Re: how to stop inserting value in db clicking on refresh bu
Posted: Wed Aug 04, 2010 6:10 am
by superdezign
dejvos wrote:I think it is not possible via PHP.
Who told you that?
This is definitely possible in PHP. There are two accepted methods of doing this. One method requires that the data that is being entered is considered unique by your database. For instance, if you were developing a blog, it's safe to assume that every blog post will be different. Then, you can check the database for a record matching the record that you are about to add (without the unique qualities such as the time that it was posted). If any records are found, you do not add it to the database and inform the user that they have already submitted that information.
Another method is to redirect to another page after the form has been successfully submitted. When the form is submitted, though the page may look the same, it is actually not. The difference is that the page after the submission also includes POST data, which is a physical part of the response from the server. By redirecting to another (or the same) page, you are effectively replacing the page + POST data with just a page. This makes refreshing harmless.
You can implement a redirect in PHP by using the Location header in PHP's
header() function:
Code: Select all
header('Location: http://website.tld/path/to/file');
Re: how to stop inserting value in db clicking on refresh bu
Posted: Wed Aug 04, 2010 6:39 am
by dejvos
About assumptions ->
in case of the normal form it is not a good to assume anything -> only thing which is relevant is to check constraints. So If will be given that in your blog must be every post unique, so you can check that, otherwise you can't assume.
About redirection->
I had the problem with this solution in older browsers.
Re: how to stop inserting value in db clicking on refresh bu
Posted: Wed Aug 04, 2010 6:48 am
by superdezign
dejvos wrote:About assumptions ->
in case of the normal form it is not a good to assume anything -> only thing which is relevant is to check constraints. So If will be given that in your blog must be every post unique, so you can check that, otherwise you can't assume.
Who are you to say that one can't make that assumption? Blog posts generally consist of a title, content, and tags at the least. If you allow your users to create two completely identical posts, then why even bother to check if they resubmitted it with a refresh?
dejvos wrote:About redirection->
I had the problem with this solution in older browsers.
Works fine in IE6. Which browsers did you have trouble with it in?