How to avoid sending PHP requests in a row?

Asked

Viewed 3,379 times

5

Well, the situation is that there are some forms on my website. These forms are programmed to trigger emails using the Phpmailer class. What happens is the following, if the page takes a little time to respond the user keeps clicking often on the send button, which generates several shots of duplicate emails. There is a way to prevent so many requests?

  • yes, you can just lock the button right after the user clicks on it, block that I speak is give a disable

  • Although this is about email, and the other about inserting DB, the vast majority of the answers of the other one has solution that serves for any repeated sending, in particular the redirect (and Phpmailer has no relation to the problem, despite being in the tags).

2 answers

7

You can use Javascript to disable the button as soon as it is clicked once, so the user cannot make multiple requests if you have Javascript enabled, what is the case with the majority, but ai depends on the type of users of your system:

Example of a button submit disabled on its own:

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

Example in Jsfiddle

  • 1

    It is also worth adding in some part of the layout a warning like: 'Wait a few moments, your email is already being sent!' =)

  • The problem is that if we validate the form somehow, the boot will be disabled...

  • 1

    You can do a function in JS to validate your form and place it in the onsubmit event of this. In this function you take the opportunity to check the field(s) and, according to the need, enable or disable the button...

  • 2

    @Cesarandre, "if we validate in any way" is already another question ;) Very elegant solution, Miguel! A detail, I had to include a form.submit() send the form to work: onclick="this.disabled=true; this.value='Enviando...'; this.form.submit(); return true;"

  • @brasofilo: good observation, I will change the response to reflect this. Thank you!

2

An efficient way to avoid multiple unnecessary requests followed is to use a captcha service, so the request is only made once and after the user sends the captcha, this avoids multiple requests and also the problem of possible spam.

However captcha ends up leaving the sending of the form slower for the user (which is unpleasant), so one solution that comes to mind is the following:

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

You can also disable the upload button with the attribute disabled using javascript, this will cause a more pleasant effect although it may be the case that the user has javascript disabled.

The script below can help:

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

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

});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.