Jquery User Defined Function Not Working

how can i define user defined function inside Yii Jquery ?

for example this is my code:


Yii::app()->clientScript->registerScript('test', "

function test(id){

alert(id);

}

});

        

");


foreach($radios as $radio){

	echo $form->radioButton($model,'Name',array('value'=>$radio->ID,'uncheckValue'=>null , 'OnClick'=>'test(radio->ID)'));

}




here the test function won’t work, please advise

Dear Friend

I hope the following would serve the purpose.

The event should be called like this.




'onclick'=>'test('.$radio->ID.')',



Regards.

i did like this but it is not calling the function

Dear Mazier

To simulate your scenario.

This is what I have done.




<?php

Yii::app()->clientScript->registerScript('test','


function test(id)

{	

    console.log(id);

}


',CClientScript::POS_BEGIN);


$authors=Author::model()->findAll();

foreach($authors as $author)

	echo CHtml::link("click","#",array("onclick"=>'test('.$author->id.')'))."</br>";

?>



Try to put CClientScript::POS_BEGIN as third parameter.

Regards.

Thanks for your Reply

Using your code now am able to execute the function. But the what i exactly need to pass the value of the radio button in one my controller function using ajax.

this is my function:




Yii::app()->clientScript->registerScript('test', "

function test(id){       

   $.ajax{

    url:'".$this->createUrl('controller/action')."',

    type:'POST',

    data:{id:id},

    success: function(result) {

        alert(result); 

    },

    });

}    

",CClientScript:OS_BEGIN);



now ajax is not working here…

Dear Friend

This is because by default YII allows GET parameters only for action parameter binding.

To overcome this problem, we can do the following.

In your controller, we have to override the CController::getActionParams.




public function getActionParams()

	{

		return $_GET+$_POST;//+ is array union operator.

	}




or

we have to make the following modification.

Inject the data into url.




Yii::app()->clientScript->registerScript('test', "

function test(id){       

   $.ajax{

    url:"'.CHtml::normalizeUrl(array("test/clickResponse")).'"+"&id="+id,

    type:'POST',

    success: function(result) {

        alert(result); 

    },

    });

}    

",CClientScript:POS_BEGIN);



Hopefully it would solve the problem.

Regards.

Great,

thank you so much for your cooperation

now it is working :)