Development Process in Yii Framework

I have an ajax submit button as follows:

echo CHtml::ajaxSubmitButton( '>', $this->createUrl('/shop/category/nextCategory&id=16&store=true&gift_store='.$_GET['gift_store'].'&startValue='.$start_value.'&endValue='.$endValue), array( 'type'=>'GET', 'update'=>'#test', 'beforeSend' => 'function(){ alert("beforeSend"); }', 'complete' => 'function(){ alert("complete"); }', ) ); 

In my action in the controller, I use the following batch as follows:

 $this->renderPartial('view',array('model'=>$this->loadModel()),false,true); 

as i set processOutput to true, the ajax submit button works fine. But it continues to execute (for the number of times I clicked). I want it to run only once when the submit button is pressed. It will be great if someone can help me with this. I have been trying to fix this all day.

+4
source share
3 answers

Ok, I decided the solution. What happens, the ajax request is launched based on the identifier. Each time a div is reloaded via ajax, a new delegate / event will be loaded, indicating the same identifier that came before it. Therefore, when you actually press the button, both delegates do their job because both correspond to the button pressed.

There are two solutions to this problem. One of them has a unique identifier each time a button is generated as follows:

 array('id' => 'next-category-'.uniqid()); 

In another way, and this is how I solved it,

 array("id"=>"next-category", "live"=>false); 

live: boolean whether to bind an event handler using live / delegate or direct style. If not installed, liveEvents will be used. Providing this send item will solve it.

+4
source

With jQuery, you can define the following code for a β€œcomplete” function:

 function() { $('input[type=submit]").attr("disabled", "disabled"); } 

Thus, upon the successful completion of your button, you will be disabled to click again if I understand you correctly.

Remember to change the input of the css [type = submit] selector to the CSS selector of your button.

Solution 2 : Wap your CHtml :: ajaxButton in

 if (!Yii::app()->request->isAjaxRequest) { } 

Then, when you download content via ajax, your button will not appear on the page

+1
source

It is better if you create normalizeUrl for an ajax request.

 echo CHtml::ajaxSubmitButton( '>', CHtml::normalizeUrl(array('/shop/category/nextCategory',array('store'=>1,'gift_store'=>1,'startValue'=>1,'endValue'=>1))), array( 'type'=>'GET', 'update'=>'#test', 'beforeSend' => 'function(){ alert("beforeSend"); }', 'complete' => 'function(){ alert("complete"); }', ) ); 

This code is perfect, I checked. It seems like they might be some error somewhere in your js code.

+1
source

All Articles