How to avoid sending PHP requests in a row? [duplicate]

5

Well, the situation is the following there are some forms on my website. These forms are programmed to fire e-mails using the PHPMailer class. What happens is this, if the page takes a while to respond the user is often clicking the submit button, which causes multiple shots of duplicate emails. Is there a way to prevent so many requests from occurring?

    
asked by anonymous 12.03.2014 / 15:13

2 answers

7

You can use JavaScript to disable the button once it is clicked once, so the user can not make multiple requests if you have JavaScript enabled, which is the case of the majority, but it depends on the type of users of your system:

Button example of submit that is disabled by itself:

<input type="submit" onclick="this.disabled = true; this.value = 'Enviando…'; this.form.submit();" value="Enviar">

Example in JSFiddle

    
12.03.2014 / 15:18
2

An efficient way to avoid multiple unnecessary requests followed is to use a captcha service, so the request is only made once and once the user has sent the captcha, this prevents multiple requests and also the problem of possible spam. / p>

But captcha ends up sending the form slower for the user (which is disgusting), so one solution that comes to mind is:

You can store in a session the exact time the last request was made, and set a minimum time limit for a new action.

You can also disable the submit button with the disabled attribute using javascript, this will cause a more pleasant effect although the user may have javascript disabled.

The script below can help:

$('form').on('submit', function() {

    $(this).find('[type="submit"]').attr('disabled', 'disabled');

});
    
12.03.2014 / 15:30