Bharanikumar
Bharanikumar

Reputation: 25733

how to disable F5 keyboard keys

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

Upvotes: 0

Views: 1521

Answers (6)

joshcomley
joshcomley

Reputation: 28818

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.

Upvotes: 1

Emil Vikström
Emil Vikström

Reputation: 91942

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.

Upvotes: 1

Gareth Saul
Gareth Saul

Reputation: 1307

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

Upvotes: 13

David Hedlund
David Hedlund

Reputation: 129792

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.

Upvotes: 5

Flatlin3
Flatlin3

Reputation: 1659

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.

Upvotes: 1

Oded
Oded

Reputation: 499002

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).

Upvotes: 2

Related Questions