Multiple Form Submission

Hi Guys, I need some help to understand my problem.

I have a form that send some info (by post) to a third party API by CURL. The controller action that does that has a redirect in both case fail or success.

My problem is that if I click twice or more the submit button, than I get two or more successful calls to that API and only after that the action is doing the redirect to the success page.

How can I prevent that on server side? I’ve tried using sessions but still doesn’t work.

The code is like this:


...

try 

{

     if ($new_user->validate()) 

     {

          if( Yii::app()->theExternalApi->pay($new_user) )

          $this->redirect(array('controller/success'));

     }

}

catch()...


// session protected code version (not working also):

...

try 

{

     if ($new_user->validate() !isset(Yii::app()->session['doubleclick'])) 

     {

          Yii::app()->theExternalApi->pay($new_user);

          Yii::app()->session['doubleclick'] = true;


     }

}

catch()...

Why doing this on server side? Lot of unnecessary work, while you can do it easily on client side. Use jQuery or pure Javascript to bind ‘click’ event to form submit button (or as alternative – bind ‘submit’ event of the form) and inside click handler for that event, simply disable button.

Quick example, without testing:


$('#button_id').click(function(){$('#button_id').attr('disabled', 'disabled')});

[size="2"]You should find millions of similar examples in the Internet.[/size]

BTW: Always use code button (second to the right – <>) to enclose example code, as if you type it directly, like you did, it is nearly completely un-readable.

Thanks Trejder.

For now JS is fine, I already did that but I’m wondering why that behaviour.