Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 24th, 2007, 11:20 PM   #1
ssrun
Newbie
 
Join Date: Aug 2006
Posts: 20
Rep Power: 0 ssrun is on a distinguished road
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.
ssrun is offline   Reply With Quote
Old Apr 25th, 2007, 12:43 AM   #2
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
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.
Booooze is offline   Reply With Quote
Old Apr 25th, 2007, 1:57 AM   #3
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
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.
Styx is offline   Reply With Quote
Old Apr 25th, 2007, 2:40 AM   #4
tAK
Programmer
 
Join Date: Mar 2007
Posts: 33
Rep Power: 0 tAK is on a distinguished road
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.
tAK is offline   Reply With Quote
Old Apr 25th, 2007, 5:45 AM   #5
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3 kruptof is on a distinguished road
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.
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Apr 25th, 2007, 10:23 AM   #6
ssrun
Newbie
 
Join Date: Aug 2006
Posts: 20
Rep Power: 0 ssrun is on a distinguished road
Quote:
Originally Posted by kruptof View Post
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.
ssrun is offline   Reply With Quote
Old Apr 25th, 2007, 12:06 PM   #7
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
Not only that, but they could hit the back button and resubmit the form data on the transfer page.
Styx is offline   Reply With Quote
Old Apr 25th, 2007, 12:29 PM   #8
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
Quote:
Originally Posted by Styx View Post
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.
Booooze is offline   Reply With Quote
Old Apr 25th, 2007, 12:32 PM   #9
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 420
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
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.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Apr 25th, 2007, 3:00 PM   #10
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
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]
Styx is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
FFT DWTIB -> Optimization -> Choosing An Appropriate Run Time Sane Software Design and Algorithms 7 Dec 1st, 2006 10:40 AM
Show or hiding forms/modifying control properties ..from different a form.. cloud- C# 4 Nov 10th, 2006 10:51 AM
Form Submit Blues MegaArcon Python 3 Dec 14th, 2005 4:20 PM
.NET Timer Form closing issue MBirchmeier C# 4 Nov 21st, 2005 10:00 AM
entering data into excel from a form glevine Perl 1 Nov 18th, 2005 5:03 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:31 AM.

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