anyway a page reload won't repost data

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
kidbrax
Forum Newbie
Posts: 2
Joined: Sat Feb 15, 2003 8:51 am
Location: Austin, TX
Contact:

anyway a page reload won't repost data

Post by kidbrax »

I have a script that inserts a record to a table based upon a user's form data(uses POST method). When they submit the form, a page loads showing the records in the table. Whenever this page is reloaded, a new record is inserted. Can this be prevented without using SQL to check for duplicate records? Is there a way to keep the page from reposting the form data?
Bitmaster
Forum Newbie
Posts: 20
Joined: Thu Nov 21, 2002 8:42 am

Post by Bitmaster »

If you cant find a way to detect attempts to add duplicate database records (examine your table's primary keys), you can try the following approach: After processing the request (and writing to the database), send a redirect to another .php page that will display the new record. Dont output anything except the

Code: Select all

header ('Location: http://myserver/...')
from the page that writes to the database.
kidbrax
Forum Newbie
Posts: 2
Joined: Sat Feb 15, 2003 8:51 am
Location: Austin, TX
Contact:

can you flush the POST data

Post by kidbrax »

I have thought and will do it if i have to, but I guess what I really want to do is somehow flush the POST data, so the browser thinks there was no form submitted when the page is reloaded.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I'd probably do the header thing myself but this might be what you're looking for (note I didn't test it):

Code: Select all

<?php
function post_array_processor() {
    foreach ($_POST as $key=>$value) {
        $_POST[$key] = null;
    }
}
?>
In your form processor script, you'll probably need an IF step before the INSERT so that you don't add empty rows to the database.
Last edited by McGruff on Thu Aug 11, 2005 3:03 pm, edited 1 time in total.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Set a session varaible, and everytime the page reloads make it so that it check whether that varibale is already set or not. If set print a message.
Post Reply