0

Am doing the online cab booking services,

once user reached the successfully completed his journey,

we are showing the thank for booking and we show the booking id, some people they hit the F5

key, so page get refresh and the new entry will inserted ,

So i want to deactivate F% on my cashthankyou you page,

Thanks Bharanikumar

Bharanikumar
  • 25,457
  • 50
  • 131
  • 201
  • 1
    Note that there is not just Windows. Other operating systems have other combinations like *Control+R* or *Command+R* for example. – Gumbo Jan 13 '10 at 11:21

6 Answers6

13

You won't have much luck with this - control / detection of key presses is heavily browser dependant, and overriding standard behaviour is usually impossible.

Rather than this approach, you need to detect and appropriately handle duplicate form submission:

How to handle multiple submissions server-side

Community
  • 1
  • 1
Gareth Saul
  • 1,307
  • 1
  • 9
  • 19
  • +1 Seconded. This is a common problem and it has solutions that do not involve tampering with the user's UI. – Pekka Jan 13 '10 at 11:18
5

The best option is usually to find a way that don't involves tampering with browser functionality. In your case, that would be making the user submit the booking to a page that inserts the entry, and then redirects the user to a thankyou-page that does nothing more than displaying that. The user would then be able to refresh the page any amount of times, without anything dangerous happening.

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • This is not a good solution as it doesn't handle double-clicks, and it doesn't prevent using the back button in the browser. =/ – Emil Vikström Jan 13 '10 at 11:21
  • well i guess the example solution could be better picked, yes, but my main point was still that you *shouldn't* disable the f5 key (nor should you prevent using the browser's back button for that matter), but you should seek to handle any user behavior in an appropriate manner, without creating any duplicate bookings. – David Hedlund Jan 13 '10 at 11:24
  • @Emil Vikström: With regards to double clicks, you can use a token system to prevent multiple submissions being processed. Also, the back button makes no difference if the redirection is done properly: the form should send the browser to an intermediary script that processes it and then immediately sends a `Location` header to the browser, redirecting to the success page. If the user then goes back, he/she will end up on the form page. – Will Vousden Jan 13 '10 at 11:32
2

You can't. This is in how the browser is coded and you can't disable it from the webpage.

You need to restructure your application to identify a refresh of this kind and not creat an additional record.

One way to do that is to check if a record was entered for the user just several seconds ago and if that is the case, not insert a new record.

Another way it to add an interstitial page that will do the adding then redirect to your confirmation page (this page is just a display page and refreshing it won't do anything).

Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

I dont't know if you can disable the F5 but can display some kind of "are you sure" message.

This can be done using window.onbeforeunload which is called before the window reloads or gets closed.

Flatlin3
  • 1,658
  • 14
  • 26
  • 2
    a reload that posts post-data in most browsers already comes with a warning, so i guess the user has already elected to disregard this :) – David Hedlund Jan 13 '10 at 11:17
1

There could be a couple of reasons they refresh the page. Maye they used their back button, double-clicked on your submit button or anything else that does the loading twice.

Here are two real solutions to your problem:

1) Put in a form field with a random number, save this number along with the booking and then check against your booking table if there already are a booking with that value. This will stop them from sending the form twice.

2) Save a cookie with the last time they completed a booking. Check this value and don't allow a new booking for i.e. five minutes.

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
1

An alternative would be to redirect your user to the thank you page, loading the ID from the session.

This way, when the user hits F5 the thank you page will load and no form submission will be attempted again.

If no booking ID is in the session when the thank you page is loading, redirect back to the home page or a suitable error page.

joshcomley
  • 28,099
  • 24
  • 107
  • 147
  • This does not prevent the user from using the browser back button. – Emil Vikström Jan 13 '10 at 11:55
  • @Emil - The OP didn't ask about the back button, only F5. Dealing with the back button is out of the scope of the question, but a solution could easily involve checking for a booking ID in the session. – joshcomley Jan 13 '10 at 13:12