m7dmh
(M7d Mh)
February 12, 2013, 8:48am
1
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
seenivasan
(Chellamnivas)
February 12, 2013, 10:08am
2
Dear Friend
I hope the following would serve the purpose.
The event should be called like this.
'onclick'=>'test('.$radio->ID.')',
Regards.
m7dmh
(M7d Mh)
February 12, 2013, 10:17am
3
i did like this but it is not calling the function
seenivasan
(Chellamnivas)
February 12, 2013, 10:31am
4
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.
m7dmh
(M7d Mh)
February 12, 2013, 11:15am
5
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…
seenivasan
(Chellamnivas)
February 12, 2013, 12:49pm
6
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.
m7dmh
(M7d Mh)
February 12, 2013, 3:45pm
7
Great,
thank you so much for your cooperation
now it is working