OnClick - prevent user from multiple submits [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to prevent multiple form submit from client side?
We have a project management tool which we are using, but when a user submits a log under task they click the submit button several times. this adds in several entries of the same information.
I have tracked down the code within the PHP file and I have seen online many solutions for on submit click. I have been playing around for the last four or five hours but unfortunately I have been unable to get anything to work. I was hoping someone could provide a little assistance:
This is the code that I have in the PHP file:
<input type="submit" class="button" value="<?php echo $AppUI->_('update task'); ?>" onclick="updateTask();" />
effectively I just want the user to click the button, and after a single click. It is either greyed out or the user is unable to click any more.
Thank you

Just disable the button in updateTask() function:
document.getElementById("mysubmit").disabled = true;
This assummes you add ID to your submit button.
If it's an AJAX submit, make sure you reenable the button if the submit fails.

Why not simply disable the button in your updateTask() script?
document.getElementByID("myButton").disabled='disabled'
If the updateTask function is too generic, then your button could call a more specific function that deals with just that button:
function myButtonClicked() {
document.getElementByID("myButton").disabled='disabled';
updateTask();
}
and then change your button to call myButtonClicked instead of updateTask.
<input type="submit" ... onclick="myButtonClicked();" />

Related

How to disable button when clicked button to action window location and enable it again after window location finished [duplicate]

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?
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..."
(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)"/>

Knowing Which Button Submitted a Form (PHP and Javascript)

I have a button that I'm using to submit a form (quiz). I also have another button to do this, but it gets built dynamically. Note the workflow of the code below.
Ultimately, I want PHP to know which button was pressed to submit the form. So, I included name="save_progress" in the <button> code below. However, including that automatically submits the form and bypasses the setTimeout() in my javascript. Removing it makes the setTimeout() function properly, but I don't get the save_progress data via $_POST.
Button...
<button class="btn btn-primary btn-block btn-lg" name="save_progress" onclick="save_progress(); return false;">Save Progress</button>
Javascript...
//For saving quiz progress
function save_progress(){
$('#save_progress_submit_container').modal('show');
setTimeout(function() {
submit_quiz();
}, 1000);
}
//Submitting a quiz
function submit_quiz(){
$("#answers").submit();
}
Any ideas that will work with this workflow? I've already reviewed this (How can I tell which button was clicked in a PHP form submit?) and it doesn't apply here, unfortunately.
Came up with a better solution overall. I created <input type="hidden" id="submit_type" name="submit_type" value=""> in the form, then simply updated the value using jQuery based on which button was clicked.
For future readers, #Niloct's link in the comments caused me to get a save_progress() is not a function error, but it did prevent the submission.

How to make a button clickabled when hovered over

This question is going to sound strange to many of you. I need a button where it can't be triggered by a click. That means it won't do an action. Like if the form attribute action is set to something like next.html the click won't cause it to go to the next page.
And when the user hovers over the button, it can go to the next site. The reason why I am doing this is because a bot can submit data without hovering over the button. I am hoping this will prevent bots from submitting anything into my site.
I don't really have any code, but is there any way to do this in Javascript/jQuery?
If this confusing please ask more questions in the comments and I will try to answer to the best of my capabilities.
Now I am not sure how effective this technique would be at blocking bots. If you still want to give it a go, I'd do something like this:
HTML:
<form class="form" action="1.php" type="post">
<input type="text">
<input class="submitbutton" type="submit">
</form>
Javascript/jQuery:
var $form = $('.form'),
$btn = $('.submitbutton');
// Disable submit button on page load
$btn.prop('disabled',true);
// Reactivate submit button on form hover
$form.hover(
function(e){
e.stopPropagation();
$btn.prop('disabled',false);
}, function(e){
e.stopPropagation();
$btn.prop('disabled',true);
}
);
I put together an example at JSFiddle.
Bots search for the <form> element
Bots query for the form's action value
Bots won't follow your form action if there's no form in your page. If that might sound strange:
Create your entire form using JS (No form? No bots.)
Either way (specially if you care about noJS visitors) you need to validate your form
on server side (that's what matter the most)
on client side (JavaScript; notify your users if they forgot to fill something - typos)
Here you can find an approach example
Why your hover approach/intent is bad:
You'll be only messing with UI creating a bad UX. Nothing more.
The form is already there on the page revealing all what a bot needs.
The bot does not need any button to submit your form.
Some users might use the TAB key to focus the SUBMIT button - so there's no hover involved whatsoever, just a poor form that does not work as it should.
In general, clicking the button should submit the form. If you want to force the issue, I think you should try to disable the button first.
<span style="padding: 8px; background: red;" onmouseout="this.firstChild.disabled='';"><input type="button" name="test" id="test" value="roll over me" onmouseover="this.disabled=true;"></span>
as provided in the answer here:
Javascript: enable/disable button with mouseover/mouseout
it seems your question is navigating to another page without clicking a button.
<html>
<head>
</head>
<body>
<button onmouseover="go()" id="button">hii</button>
<script>
var anchor=document.createElement("a");
var button=document.getElementById("button");
anchor.href="alarm2.html";
function go()
{
button.onmouseover= anchor.click();
}
</script>
</body>
</html>

onclick submit button JSP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Multiple submit Button click problem?
Have a type=Submit button in a JSP. Requirement is to disable this button on click.
The basic requirement is to stop posting multiple requests of the same form if this button is clicked multiple times for the same form.
Thank You.
Even if you disable the submit button the user can still submit the form by pressing F5 or ctrl+F5 or even using Browser reload button.So disabling the submit button won't be of much use.
Why not use Post/Redirect/Get method.
The PRG pattern basically redirects the user to another page when it is Posted.
Hence now when the user reloads or tries to resubmit the form he is calling the Get method so the form is prevented from resubmission.
However this is also not fully secured approach.
Look here: http://www.developertutorials.com/tutorials/javascript/how-disable-submit-button-050416-1275/
I use a simple JS function you can invoke with onlick():
<script type="text/javascript">
var isSubmitted = false;
function safeSubmit(entityName) {
if (!isSubmitted) {
isSubmitted = true;
document.forms[0].submit;
}
}
</script>

Disabling an HTML button while waiting for a result

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?
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..."
(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)"/>

Categories

Resources