0

I'm using a jQuery to submit the data from form to my controller file.

It is adding a favourite piece to the database, but, how to prevent user from sending the data trought ajax script too frequent?

I mean, I can click on the submit button in my form thousand amount of times - how to prevent that? So it will be for example, 1 request per 10 seconds?

Lucas
  • 3,517
  • 13
  • 46
  • 75
  • You would have to prevent that in the file being called, because what AJAX does is just a simple request, which you could also call by typing in the URL in the address bar. You could do a simple IP or cookie check in the file being called. – dan-lee Apr 14 '12 at 15:46
  • You could disable the button when they click it, and when you receive your AJAX response add a delay callback function to reenable it. – Andy Apr 14 '12 at 15:49
  • @Andy Could you post an example with how it should look like? – Lucas Apr 14 '12 at 15:50

1 Answers1

0

Just off the top of my head I would say something like this:

$("#button_id").one('click', DoSomething);    // attaches the function for ONE click

function DoSomething() {
    $.post("test.php", function(data) {
        alert("Data Loaded: " + data);
        $("#button_id").delay(10000).one('click', DoSomething);   // reattach the listener 
    });        
}

Source copied from this question and the jQuery manual

Community
  • 1
  • 1
Andy
  • 2,095
  • 1
  • 29
  • 59