Ajax Submit Button Issue

4777

ajaxButton.png

In image showing two ajax button showed. While page loading ajax button id created like yt0,yt1,yt2… etc.

then I loaded part of page using ajax then again it created ajax button yt0,yt1, like that…

I want to create another ajax button series like yw0 or something like that…

How to do that???

I suppose you want to identify the buttons, then, why don’t you set id property for AjaxSubmitButton?




CHtml::ajaxSubmitButton(

     'the button label',

     'the URL for the AJAX request',

     'AJAX options',

     array('id' => 'desiredId') // HTML options for the button.

     );



Regards.

Yii is a php framework, the CHtml::ajaxbutton is intended to be an helper, not an althernative to jquery ajax.

If you are going to do more complicated works with ajax, be prepared to write your own js code instead of use yii helpers.

Thanks for your reply…

I cant give ID as static bcoz like that more ajax content will get load, then every button id will be the same.

It wont work unless I give dynamic id.

This is a typical problem of the helpers.

The helpers get a dynamic id like yt0, yt1 and the counter is restarted at each render.

So you load ajax content that has the same counter and the same id.

A pure js solution is not to use the yii helpers, which are great but does not cover all cases, and use a js instead.

For example you can give a class to the button and then register the script:


$('body').delegate('.myclass', 'click', function(e){

   e.preventDefault();

   //do something

});

If all the button are suposed to do the same you can just delegate, as delegate works with all instances, even "future" one.

Hi,

<?php echo CHtml::ajaxButton(‘ajaxbutton’,

				Yii::app()-&gt;createUrl('post',array('pid'=&gt;&#036;post-&gt;post_id)), 


				array(


				'type'=&gt;'POST',


				'beforeSend' =&gt; &quot;function(request)


				 {


				   //alert('heelo'+request)


				 }&quot;,


				'success' =&gt; &quot;function(data)


				 {


					//alert (data)


					 &#036;('.form').html(data);


				 }&quot;,


				 'error' =&gt; &quot;function(data)


				 {


					// handle return data


					alert('erro'+data);


				 }&quot;,


				'data' =&gt; 'js:&#036;(&quot;#detail-form&quot;).serialize()',


				),


				array('id'=&gt;'scopy'.rand(),'live'=&gt;false,


				'href' =&gt; Yii::app()-&gt;createUrl('post',array('postid'=&gt;&#036;post-&gt;post_id))


				)


				); 


			?&gt;

Try this way. I hope it should work.

Thanks

chandran nepolean