Page 1 of 1

anyway a page reload won't repost data

Posted: Sat Feb 15, 2003 8:51 am
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?

Posted: Sat Feb 15, 2003 9:46 am
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.

can you flush the POST data

Posted: Sat Feb 15, 2003 10:37 am
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.

Posted: Sat Feb 15, 2003 12:15 pm
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.

Posted: Sat Feb 15, 2003 1:37 pm
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.