Ajaxsubmitbutton In Cactiveform

Hi everybody,

after spending hours reading about the issues with the ajaxSubmitButton here in the forum without finding a solution for my problem, I am going to describe it here.

I have successfully implemented a ajax-request as described here:

CJuiDialog for create new model and I make use of the code described here:

Working with CGridView in Admin Panel

In the latter POST data is passed with an ajaxSubmitButton to a controller-action and this works fine. In the former data is sent from the controller-action and this works fine either. But when I want to put it alltogether no POST-data is passed to the controller anymore.

My code looks like this:




<?php

$form=$this->beginWidget('CActiveForm', array(

'id' => 'product-form',

    'enableAjaxValidation'=>true,

));


$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'product-grid',

	'dataProvider'=>$productDataProvider,

	'columns'=>array(

		array(

            'id'=>'autoId',

            'class'=>'CCheckBoxColumn',

            'selectableRows' => '100',   

        ),

		'product',		

		'description',

		'price',

		'buttons'=>array(

			'update'=>array(

				'url'=>'Yii::app()->controller->createUrl(\'product/update\', array(\'id\'=>$data["id"]))',

			),

	  	),

	),	

)); 


// This button works fine

echo CHtml::ajaxSubmitButton('Delete',

array('product/ajaxupdate','act'=>'doDelete'), 

array('beforeSend'=>'function() { if(confirm("Are You Sure ...")) return true; return false; }', 'success'=>'reloadGrid'));


// This button does not submit POST data

echo CHtml::ajaxSubmitButton('Do it', array('product/test'), 

		array( 	'data'=>'js:$(this).serialize()',

				'type'=>'post',

				'dataType'=>'json',

				'error'=>'function() {alert("Something went wrong.");}',

				'complete'=>'function(){$("#relation-dialog").dialog("open"); return false;}',

				'success'=>'function(data){$("#relation-dialog div.divForForm").html(data.div);}',

		));

$this->endWidget();

echo CHtml::endForm();


?>



I have also tried a SubmitButton.




<?php echo CHtml::submitButton('Do it',

		array('id'=>'submit_button')); ?>



With some jquery:




$(document).ready(function(){

	$('#submit_button').click(function(){addRelation();});

});



And then the addRelation() function as described in the tutorial by zaccaria which as I said before works just fine in another script of mine.

What am I doing wrong?

Thanx in advance for any suggestion.

I think I have solved the problem.

First I replaced the CHtml::SubmitButton with a CHtml::Button.

2nd I added an onclick event: array(‘id’=>‘submit_button’, ‘onclick’=>‘addFunction();’)); to that CHtml::Button.

And 3rd I found out, that the ‘this’ in the code below does not refer to the input data of the form.




<?php echo CHtml::ajax(array(

    'url'=>CController::createUrl('controller/action'),

    'data'=> 'js:$(this).serialize()',

    'type'=>'post',

    'dataType'=>'json',

     ...



So I changed one line to:




'data'=>array('ajaxData'=>'js:$(".checkbox_id :checked").serialize()',



I have added a css.class ‘checkbox_id’ to the CCheckBoxColumn to be able to omit the first checkbox.

And all of a sudden my POST variable was full of data :wink: