Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   How to handle form reloads (http://www.programmingforums.org/showthread.php?t=13056)

ssrun Apr 25th, 2007 12:20 AM

How to handle form reloads
 
Hi,

I have a form that submits data to a script that adds the information to a table and then the script displays the added information on the screen. Now, if the user hits the "Refresh" button on the browser the script is executed again and another record gets added with the same information.

I was wondering how to go about handling this issue so that if the user hits "Refresh" the script will not do anything.

Booooze Apr 25th, 2007 1:43 AM

When the user submits the form, return the data with a hidden value. Check for that hidden value when the form is submitted. Then simply don't update the data, just return what was sent.

Styx Apr 25th, 2007 2:57 AM

That won't work. The hidden variable will be available for the next page, not the page they submitted. Or if you're talking about it being there from the start, that wont work either, as the hidden variable would be resubmitted as well.

Set a cookie when it's submitted. Then, check if the cookie is set. You could also use sessions, or checking if the submitted data to see if a similar data set is already in the database or was just submitted.

tAK Apr 25th, 2007 3:40 AM

I always check to see if the database already has a set of matching records, assuming your input contains 1 piece of data that always has to be unique, a username for instance), then use that.

A cookie would also work, BUT, if someone submits then deletes their cookies and refreshes, it would most likely be entered again.

kruptof Apr 25th, 2007 6:45 AM

Why don't you have a page that scripts goes to, this page will update the table(s) then this page will go to another page where you get information from the table and display it. That way if they do decide to refresh only the get data operations will recur.

ssrun Apr 25th, 2007 11:23 AM

Quote:

Originally Posted by kruptof (Post 127124)
Why don't you have a page that scripts goes to, this page will update the table(s) then this page will go to another page where you get information from the table and display it. That way if they do decide to refresh only the get data operations will recur.

Thanks I was thinking about that too but I wanted to try and accomplish it with as few pages as possible.

Styx Apr 25th, 2007 1:06 PM

Not only that, but they could hit the back button and resubmit the form data on the transfer page.

Booooze Apr 25th, 2007 1:29 PM

Quote:

Originally Posted by Styx (Post 127120)
That won't work. The hidden variable will be available for the next page, not the page they submitted. Or if you're talking about it being there from the start, that wont work either, as the hidden variable would be resubmitted as well.

Set a cookie when it's submitted. Then, check if the cookie is set. You could also use sessions, or checking if the submitted data to see if a similar data set is already in the database or was just submitted.

Yes it would. If you set a hidden value to false on the page, and submit it, all you have to do is test the value. If it comes out false, you know it's the first submission, so you process it. But then when you redisplay the page, just set that hidden value to true. Therefore, it won't be processed again. If it test true on the update page, just redraw the data again. Same effect as the cookie, sort of, but won't last if you close your window. It also comes down to how you program it. Cookie would probably be best though.

grimpirate Apr 25th, 2007 1:32 PM

I think Booooze idea would work just fine, for instance here's a sample instead of using a hidden variable using the $_GET superglobal:
:

if(isset($_GET['foo'])){
... display the table with its updated database values
... display a URL link to the same page but with the submission form
}else{
... display the table
... display the submission form whose action method should go to the same page but appended with '?foo=1'
}

This way the user has to consciously click to get to the submission form and submit data, and its still the same page. So even if they keep refreshing no new data is passed.

Styx Apr 25th, 2007 4:00 PM

could you give an example perhaps?

Here's what I'm thinking based on this:
[php]<?php
if (isset($_POST['test']) && $_POST['test'] == 'false')
{
// process form
// to know the difference between whether it was the first submission or
// the second submission, you'd have to check something such as the database
// otherwise, there's no difference
}
<form method="post">
<input type="hidden" name="test" value="false">
<input type="submit" name="submit" value="Submit">
</form>[/php]


All times are GMT -5. The time now is 2:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC