473,386 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Refresh problem and posts

I have a problem with refresh.

I have a contact form that sends an email when the submit button is
pressed. I have a hidden variable and the code for emailing is only
executed if the variable is set. The variable is cleared at the end of
the code. All works as expected unless the REFRESH button is pressed
by the user in which case the script is executed and the emails are
sent once more. Debugging shows the test variable is set again even
though it was unset before exiting on the previous run. How can this
be?

I know there are some things I could do like using database entries or
cookies to hold the variable or even jump to another page which I do
not want to do.

There must be a simple explanation and hopefully solution to this.

Thanks

--
John
Sep 18 '05 #1
5 1870

John wrote:
I have a problem with refresh.

I have a contact form that sends an email when the submit button is
pressed. I have a hidden variable and the code for emailing is only
executed if the variable is set. The variable is cleared at the end of
the code. All works as expected unless the REFRESH button is pressed
by the user in which case the script is executed and the emails are
sent once more. Debugging shows the test variable is set again even
though it was unset before exiting on the previous run. How can this
be?

I know there are some things I could do like using database entries or
cookies to hold the variable or even jump to another page which I do
not want to do.

There must be a simple explanation and hopefully solution to this.


the browser actually saves all of those POST variables... If the user
requests the page again, the way the browser displays the same page, is
to resend the same variables.

there are several solutions. one is this:

1. when form submitted via post, send the email, then
2. imediately redirect to the same page, maybe with a querystring
paramater of emailsent=1 and username=Joe Public.

When the page is displayed with the GET emailsent parameter set to 1,
then you can say "Thanks you Joe Public for sending the email." or
whatever.

Sep 18 '05 #2
>I have a contact form that sends an email when the submit button is
pressed. I have a hidden variable
What kind of variable? HTML form? Javascript? PHP?
and the code for emailing is only
executed if the variable is set.
What kind of code? PHP?
The variable is cleared at the end of
the code.
Resetting a $_GET or $_POST variable is meaningless after the PHP
code finishes executing. If the page is submitted again, it
gets its new value from the page. Ordinary PHP variables vanish
when the code exits.

I'm not so sure Javascript variables retain their values in this
situation either.

$_SESSION would retain its value if you properly start up a session
at the beginning of the PHP code.
All works as expected unless the REFRESH button is pressed
by the user in which case the script is executed and the emails are
sent once more. Debugging shows the test variable is set again even
though it was unset before exiting on the previous run. How can this
be?

I know there are some things I could do like using database entries or
cookies to hold the variable or even jump to another page which I do
not want to do.
I doubt cookies would help in this situation, except for their
use in establishing a session.
There must be a simple explanation and hopefully solution to this.


Database and/or session variables.

Gordon L. Burditt
Sep 18 '05 #3
Gordon is right...you need to keep some sort of state -- either in a
session variable or cookie.

What I usually do (and believe me, it isn't perfect) is to set some
random identifier at the top of the page. Call it $curPageId.
Whenever I draw a post form, I include the value of curPageId as a
hidden post parameter. At the top of the next page, set another
session variable called $prevPageId to the value of $curPageId, and set
a new random value for $curPageId. Then, before processing the form,
check that the value of $prevPageId from the session is the same as the
value of the form variable that was submitted. If it is, you can
process the form. If they are different, you know that the form was
refreshed.

Of course, if you want to store the value in a cookie instead of a
session, that's fine, too -- although I tend to prefer the session.

As I said before, the approach isn't perfect. You can, of course, run
into problems with multiple windows or tabs viewing the same pages.

Sep 18 '05 #4
Following on from John's message. . .
I have a problem with refresh.


You have to understand that scripting web pages is like sending letters
in the mail between people. It isn't like a phone conversation. In
fact it is more like a coupon in a newspaper which you cut out and send
for your free starter bag of toenail clippings or whatever. There is
nothing to stop somebody sending the form twice. It is up to you in
your office (ie server-side) to weed out the double clickers and robots.

OK so you don't want to be bothered and don't want to send them to
another page[1] - Sorry chum, it goes with the territory. Anyway, it
isn't difficult and _even after_ you have tested your pages with the
most daft (not to mention malicious) input you will still find odd ways
in which it gets abused.

Have a look at the posts to this ng in the last 10 days for the sorts of
things to look out for. At the very least you will want to keep a log
of what's been going on so you can spot abuse. If your site is
commercial then FX:Sharp intake of breath. Does Blogg's the builders
really want to be the subject of an investigation for sending out
pictures of 10 year old girls? - Yes, it could happen there are bots
looking for innocent sites that haven't got a clue they are being used
to channel spam/porn/u-name-it.

[1] Actually not necessary but it is REALLY IMPORTANT to tell somebody
that something has happened as a result of the clicks they've just made.

--
PETER FOX Not the same since the e-commerce business came to a .
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Sep 18 '05 #5
On Sun, 18 Sep 2005 18:14:20 +0100, John <yuiouio@dfghfgh> wrote:
I have a problem with refresh.

I have a contact form that sends an email when the submit button is
pressed. I have a hidden variable and the code for emailing is only
executed if the variable is set. The variable is cleared at the end of
the code. All works as expected unless the REFRESH button is pressed
by the user in which case the script is executed and the emails are
sent once more. Debugging shows the test variable is set again even
though it was unset before exiting on the previous run. How can this
be?

I know there are some things I could do like using database entries or
cookies to hold the variable or even jump to another page which I do
not want to do.

There must be a simple explanation and hopefully solution to this.


Thanks as ever for a great response and new ideas.

I did as Douglas suggested and reloaded the page with a GET which I
could test for. It also provided a thank you page which can then be
refreshed with no repercussions as Peter advised.

BTW its not about not being bothered but finding elegant solutions.

My thanks to a great group once again.

--
John

Sep 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Mark | last post by:
I have a working PHP/MySQL application used for data entry. The data entry screen includes a "Save" button. The PHP code for this button looks like this: if (isset($_POST)) { if ($_POST ==...
1
by: Michael Minter | last post by:
I'm just new at this so any help more than appreciated. I have created an email form in which all fields must be completed. If all the fields are not completed "submit" causes a refresh using...
6
by: Arnold | last post by:
Hey there, I've browsed for some fixes to this but only found a couple of similar posts, which didn't solve my problem. I'm wondering how to refresh an image control in a form after a user...
2
by: Raj | last post by:
Hi All, I have a problem with trying to refresh the parent window from child window in order to update data in the parent window. The sequence of events are 1) I click a button in the parent...
1
by: ppatel | last post by:
Problem I have a problem with web image button control click event. The click event does not get trigger until it has not been clicked once or page refresh occures(which is fine). When click...
8
by: TS | last post by:
Im in a web page and call an asynchronous method in business class. the call back method is in the web page. When page processes, it runs thru code begins invoking the method then the page...
4
by: Simon | last post by:
Dear reader, If I change the content of a field in an event procedure and in the same procedure I do a refresh, the refresh has no effect. The code in the event is as follows:
5
by: tshad | last post by:
I have an interface I am using to get access to some of the objects on my form: a textbox (Status) and my statusbar (StatusBar). In my class, which is actually in another class from my form I have...
6
by: Peter | last post by:
I have a APS.NET 3.5 webpage which calls a web service. What I need is to update this page automatically when a value changes in the webservice, does anyone have an example? Thank You ...
4
by: Peter | last post by:
ASP.NET 3.5 I have a web page and this web page has a DataList, when user hovers the mouse over a picture in the datalist a popup appers, this popup is a User Control and it has a cancel button....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.