Submitting form more than once!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dbazoo
Forum Newbie
Posts: 3
Joined: Wed Sep 12, 2007 6:37 am

Submitting form more than once!

Post by dbazoo »

Good day,
I know it is very simple for you all, but i am very confused how I can prevent inserting records again and again when i refresh(F5) the "add data" page. I have a form which adds data into db. When i submit the page, it goes to processing page, i.e. processingpage.php. But everytime i refresh(F5) that page, it inserts another record into db. can anyone please advise how i can prevent that?

thanks to all
Azay.
Last edited by dbazoo on Thu Sep 13, 2007 1:59 am, edited 1 time in total.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
But hard to understand how your doing that.. but why not have a simple check to see if that data is already in the database?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Check for the data, add a unique id into the POST data, or do a "page replace" with the 'Location' header.

Code: Select all

header('Location: http://domain.tld/path/to/form.php');
You may prefer to create a "success" or "thank you" page.
dbazoo
Forum Newbie
Posts: 3
Joined: Wed Sep 12, 2007 6:37 am

Submitting form more than once

Post by dbazoo »

Thanks to all the replies.
Actually, I want to put a check in my php itself and not in Sql side. And a correction, I am submitting the form to iteself i.e., index.php. When a user submits a form, it submits the form to itself. There are checks in the scripts that checks whether the form is submitted or not and responds accordingly. But if i refresh the page at this point, it submits the page again and thus, cause the insertion again. I want to prevent this. How can I check if it has been submitted once already?

Hope it is more clearer now. :)

dbazoo
dbazoo
Forum Newbie
Posts: 3
Joined: Wed Sep 12, 2007 6:37 am

Post by dbazoo »

I've found a solution for this (I guess) but I haven't applied it. Here is the algorithm(!).
Step1. Load index.php. It will include adddata.htm page. adddata.htm contains the form that needs to be submitted.
Step2. User fills up the form. Submits it. Scripts in index.php reads/parses it.
Step3. If everything is okay, the data is inserted into db. This time it will include adddataconfirm.htm. adddataconfirm.htm will be carbon copy of adddata.htm. Here we need to pass all variables to adddataconfirm.htm from adddata.htm and make adddataconfirm.htm submit automatically with some Javascript. So the user will never know which page is loaded or executed. All he will see is the Thank You message. Here even if he refreshes the page, it won't insert the data again, as the variables from adddata doesn't exist anymore.

Does it seem workable? Please suggest if you think it should be some other way.

cheers,
azzooo
youscript
Forum Newbie
Posts: 10
Joined: Thu Sep 13, 2007 3:22 am

unset $_POST or $_GET may help

Post by youscript »

unset $_POST or $_GET, or set it to null after insert into the db, if have check the invalidate submit
Post Reply