473,765 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1888

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******@eminen t.demon.co.uk.n ot.this.bit.no. html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.dem on.co.uk>
Sep 18 '05 #5
On Sun, 18 Sep 2005 18:14:20 +0100, John <yuiouio@dfghfg h> 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
4923
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 == "") { include ("InsertRecord.inc"); // Insert new record }
1
4052
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 response.redirect, Problem is allthe fields that were previously filled in are empty. How do I keep the values of the fields on refresh?
6
8820
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 clicks a cmd button to open the Windows API Common Dialog and selects a new file (jpeg). Images are initially loaded in the form's oncurrent event. I've tried using me.dirty = false to make the form current. Has anyone experienced this before?
2
23511
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 window to open a child window thru javascript window.open 2) I have some functionality in the child window that changes the data
1
3369
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 event fires first time it executes code associate with click event(As I expected). the problem is when you refresh page it fires a click event automatically. I looked around many posts and did not find any topics about this problem. Infact the same...
8
5880
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 unloads. When the callback method is raised, only the method in the web page is run and the page never refreshes, it seems it all happens on the server side. I am trying to refresh the constrols on the page inside the callback method, but when id...
4
2555
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
2425
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 the following: public interface IStatusDisplay { string Status { get; set; } string StatusBar { get; set; } }
6
4778
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 Peter
4
2805
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. After the user clicks on the cancel button I want the DataList to refresh its self. How can I do that - how can I tell the DataList or the parent web page to refresh it's self from the Popup / User Control?
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.