views:

305

answers:

6

When I am clicking a submit button on my HTML form, the function related to the button is called and it does its work. After completing the work a mail notification is sent which is consuming too much time. After that a confirmation message is displayed on the same HTML page (without using Ajax; i.e., the page is refreshed).

I want to avoid letting the user click the submit button multiple times in confusion during the waiting period of sending mails. So I am thinking that I should disable the button after it is pressed once.

How can I do this?

Can you please suggest any other technique to achive this goal without disabling the button?

+1  A: 

How can I do this?

You can take a look at the javascript code in this page:

http://www.codinghorror.com/blog/archives/000096.html

<input type="Button" onClick="this.value='Submitting..';this.disabled=true;" value="Submit">

Can you please suggest any other technique to achive this goal without disabling the button?

Show a busy panel:

"... Your request is being processed please wait..."

http://online.vodafone.co.uk/en_GB/assets/static/ipi_please_wait.gif

OscarRyz
Your example will fail if a user presses the enter key in a single line text box. You should use the onsubmit event in the form tag.
Langdon
Nice: `<form action="file" method="post" onsubmit="this.submit_button.disabled = true;">` http://stackoverflow.com/questions/2067042/disabling-an-html-button-while-waiting-for-a-result/2067103#2067103
OscarRyz
A: 

Assuming you don't want to disable the button you could always pop up a modal on the page. This will block the user's interaction with the page. You could throw some kind of loading spinner in there with a message that the submit is in progress.

Parrots
+5  A: 

Simply:

<form action="file" method="post" onsubmit="this.submit_button.disabled = true;">
    <input name="submit_button" type="submit" value="Submit" />
</form>

You can achieve this without disabling the button by using a nonce, however it is a bit more complex. Essentially, when the user requests the page that has the form that will be submitted, assign a unique id to that user's request (store it somewhere on the server, and make sure it's submitted along with the form). When the form is then submitted, look up the unique id to make sure it's not in process or already processed, and then if it's OK to proceed, mark the unique id as "in process", process the form, and then mark it as processed. If when you do the initial check and the page is in process or already processed, you'll need to take the necessary action (redirect them to a confirmation page if it was successfully processed, or back to the form if it was not successfully processed).

Langdon
+1: For mentioning a server side technique
Dave
A: 

I don't understand why it is a problem, as you are doing a regular submit, the user should see a white page while you are processing in the back end.. But in case if you want to disable the button, here is the code, use it on the button

onclick="this.disabled=disabled"
Teja Kantamneni
This will block submitting the parent form.
BalusC
A: 

You could have the button be disabled, but still seem active to the user. In the function that gets called after the button is hit the first time, have the first thing it does set a global variable like disableButton to true. When the user presses the button, have that go to a function called something like checkSubmitStatus. If disableButton = true, return false. if disableButton = false, trigger the submit function.

You have still disabled the button, but your users can press away unaware.

Anthony
A: 

If you disable a button right before submitting, then the parent form will not be submitted. You need to disable the button after submitting. Best way it to use JavaScript's setTimeout() function for this.

<input type="submit" id="foo" onclick="setTimeout('document.getElementById(\'' + this.id + '\').disabled=true;', 50);">

50ms is affordable enough to give the form the chance to get submitted.

To enhance the user experience more, you could of course append a message or a loading image dynamically during the same onclick event as already suggested by others.

BalusC