CHtml::ajaxButton not working

Hi…

Getting very frustrated here trying to use an ajaxButton…one of the things that is annoying me is that I have the ‘same’ code working just the same on another project. It may be that I am blinded to something obvious as often happens when looking at something for too long.

The idea is that I have a CActiveForm which has a ajaxButton as its submit button. The is set via css to display: none. The reason for this is that I am displaying the form in a jQuery UI dialog box and am using the dialog button to submit the form, it looks nicer this way. So when the dialog button is pressed it triggers the click event of the ajaxButton which should submit the form. Code below. It is not working - in firebug there is no ajax call being made from the browser.

Controller Code:




public function actionAjaxSubmitRegistrationForm() {

  echo "hello"; // just to test ajax return

}



Form Code (only relevant part):




	<?php echo $formUserRegistration->labelEx($modelUserRegistration,'pw'); ?>

	<?php echo $formUserRegistration->textField($modelUserRegistration,'pw'); ?>

	<?php echo $formUserRegistration->error($modelUserRegistration,'pw'); ?>

</div>

<div>

	<?php

	echo CHtml::ajaxButton(

		'Sign Up',

		'ajaxsubmitregistrationform',

		array(

			'type'=>'POST',

			'success'=>'function(data) {

				alert('it works');

			}'

		),

		array(

			'id'=>'ajaxSubmitRegistrationForm',

			'style'=>'display: none;'

		)

	);

?>

</div>

<?php

$this->endWidget();



Dialog Javascript (only relevant part:




function showDialogRegister(data) {

	var btns = {};

	btns[data.okBtnTitle] = function() {

		//alert("clicked");

		$("#ajaxSubmitRegistrationForm").trigger("click");

		$(this).dialog("destroy");

	};

	btns["cancel"] = function(){ $(this).dialog("destroy"); };



I think that is all the code involved. Also, if anyone can suggest a better way of doing this please do, the hidden ajaxButton seems a little clumsy to me but I am pretty new to Yii and can’t think of a better way to do this at the moment - and as I said, it does work on another project.

Thanks a lot for any help

tb

Read this guide.

The trick of trigger the ajax on the onsubmit of the form solves ALL your problem, you don’t have even to put the submit/ajax button, does all JuiDialog.

thanks zaccaria - i have now have something working