开发者

Disabling an HTML button while waiting for a result

开发者 https://www.devze.com 2022-12-16 05:15 出处:网络
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

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 t开发者_C百科he 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?


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


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

Disabling an HTML button while waiting for a result


(source: vodafone.co.uk)


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.


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.


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"


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.


I'm not submitting anything, but Google Chrome 31 doesn't update the button look while calculating, so i came up with this workaround:

<style>
.btnMenu{width:70px; font-size:12px}
.btnMenu:disabled{background-color:grey}
</style>
<input type="button" class="btnMenu" value="Total" onmousedown="b=this; b.disabled=true; b.v=b.value; b.value='Calculating...'; setTimeout('updateTotals(); b.value=b.v; b.disabled=false', 100)"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消