Disabling the submit button stops the PHP / Javascript submit form

I think I have a classic problem, but so far I have not been able to find a working solution.

I have a form, the user clicks "Submit", and everything works fine using the PRG template and performs verification both on the client side and on the server side.

The problem occurs when any user (let's say that he has entered valid inputs) clicks more and then quickly before the server script completes its execution ...

I don’t get a duplicate entry because I took care of this, but the browser does not go to my “thanks for submitting the page”. Instead, it re-submits the same page with the same values, and I get the user errors that I set to warn the user that he is trying to enter data already stored in the database. Details sent in the first place are in the database, but the user does not have the opportunity to know this.

I tried to disable the submit button in the send event using jQuery, but in this case the data is not sent.

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 the feed using Javascript after the button is disabled, as well as how to handle UA with Javascript disabled.

Thanks in advance

+3
5

, , ?

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

. , , . , , . , , . , , .

.

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

​​ $_SESSION, , .

<?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();
   }
}
?>
+1

, , . , . , ASP.NET, , , , . , , - , . onsubmit false ...

$('#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;
});

, , , true : http://jsfiddle.net/XcS5L/3/

+3

return true; , .

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

, , . javascript, , . (Concurrency , - , - , flock() PHP.)

:

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

You can use a class submittedto grayen buttons or something else. This has several advantages of simply turning them off; Josh already said one. Another is that Firefox loves to remember disabled states when you click the update, which can cause your users to freeze in certain situations.

+1
source

Source: https://habr.com/ru/post/1768458/


All Articles