开发者

Disabling submit button stops form from being submitted PHP / Javascript

开发者 https://www.devze.com 2023-01-19 06:34 出处:网络
I think I got a classic problem but I was not able to find a working solution so far. I have got a form , the user clicks \"Send\" and everything works fine using a PRG pattern and doing both client-

I think I got a classic problem but I was not able to find a working solution so far.

I have got a form , the user clicks "Send" and everything works fine using a PRG pattern and doing both client-side and server-side validation.

The problem arises when any user (let's assume he entered valid inputs) clicks more then once quickly before the server script ends its execution...

I do not get any duplicated entry because I took care of that but the browser does not go to my "thanks for submitting page". Instead it re-submits the same page with the same values and what I get are the custom errors I set to warn the user he is trying to enter details already stored in the database. The details sent in the first place are all in the database but开发者_如何转开发 the user has no chance to know that.

I tried to disable the submit button on a submit event using jQuery but in that case the data are not submitted.

HTML

   <div id="send-button-container">
   <input id="send-emails" type="submit" name="send_emails" value="Send"/>
   </div>

jQuery

   $(document).ready(function(){

        $('#mail-form').submit(function(){
        $('#send-emails').attr('disabled','disabled');
        });
   });

I am wondering if I can force a submission using Javascript after disabling the button and also how to deal with UAs with Javascript disabled

Thanks in advance


Depending on server-side language, the submit button being disabled could cause problems. This is because disabled elements are not POSTed to the server. Languages like ASP.NET require the button value to be submitted so it knows what event handler to fire. What I usually do is hide the submit button, and insert a disabled dummy button after it, which appears identical to the user. Then in your onsubmit handler, you can return false and submit the form programmatically...

$('#mail-form').submit(function(){
  var btn = $('#send-emails');
  var disBtn = $("<input type='button'/>").val(btn.val()).attr("disabled", "disabled");

  btn.hide().after(disBtn);
  this.submit();
  return false;
});

Contradictory to the other up-voted answers, please note that you do not need to explicitly return true from your submit handler for natural form submission: http://jsfiddle.net/XcS5L/3/


I assume this means you are depending on the value of the submit button to service the request? That is you are checking

$_REQUEST['send_emails'] == 'Send';

This is not good practice. You should never depend on the value of the submit button because that is the just what is displayed to the user. Instead, you should add a hidden input that contains the event you want to fire. After the form is submitted, you don't need to care what the value of the submit button is and you can disable it. All other non-disabled data in the form is still submitted.

You can indeed force the submission after disabling the button.

$(function () {
   $("#mail-form").submit(function () {
      $("#send-emails").attr('disabled', 'disabled');
      window.location = '?' + $("#mail-form").serialize() + '&send_mails=Send';
      return false;
   });
});

Server side set a $_SESSION variable that keeps track of the last time they made a submission and block submissions within a certain time.

<?php
session_start();
if (isset($_REQUEST['send_emails'])) {
   if (isset($_SESSION['mail_sent'])
   && strtotime($_SESSION['mail_sent']) < strtotime('5 seconds ago')
   ) {
      redirect_to_thanks();
   }
   do_post();
}

function do_post() {
   if (do_validate()) {
      $_SESSION['mail_sent'] = time();
      redirect_to_thanks();
   }
   else {
      yell_at_user_a_lot();
   }
}
?>


You have to return true; You could try this if u want a simple button to submit the form.

$(function(){
   $('#submitID').one('click',function(){
    $('#formTobeSubmitted').submit();
    $(this).attr('disabled','disabled');
 })
});


On server side, generate a random number into each form, store the number when the form is submitted, and discard the submit if that number has already been stored earlier. When the user has disabled javascript, this is the best you can do. (Concurrency issues can be tricky as the two identical requests are handled at the same time - make sure you use some sort of locking mechanism, such as a table with a unique field or the flock() command in PHP.)

On browser side, just set a flag when the form is submitted, and discard all later submits:

$('#mail-form').submit(function() {
    if ($(this).data('submitted') {
        return false;
    } else {
        $(this).data('submitted', true).addClass('submitted');
    }
});

You can use the submitted class to make the buttons gray or something. This has a few advantages to simply disabling them; Josh already said one. Another is that Firefox likes to remember disabled states when you hit refresh, which can cause your users getting stuck in certain situations.

0

精彩评论

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