Using onClick event at Chtml::label, need help

Hi all,

I need to do export file in my side, i have to to use the code below but is doesn’t trigger the controller. Any problem with the code? can anyone give me some advice?

<div onClick="<?php CHtml::ajax(array(

	'id'=&gt;'country_list',


    'type'=&gt;'POST',


    'url'=&gt;CController::createUrl('sell/loadGame'),


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


	  


	}',

));?>">Test</div>

I think you’ll have to pass an array to createUrl like CController:createUrl(array(‘sell/loadGame’)) to make this work. Use Firebug or something like that to see the ajax requests that are made.

EDIT: I was wrong, sorry. createUrl doens’t take an route array.

I have try to use firebug to check the console, nothing is happen after i click the link.

This works for me:




<div onclick="<?php echo CHtml::ajax(array(

'id'=>'country_list',

'type'=>'POST',

'url'=>array('sell/loadGame'),

'data'=>'test',

'success'=>'function(data) {}'));?>">Hallo

I am a Test</div>



  1. You have to echo CHtml::ajax,

  2. You have to get rid of the semicolon in your success function and

  3. Set ‘data’

Interesting note: If you don’t set ‘data’ it will automatically be set by Yii with ‘data’:jQuery(this).parents(" form").serialize()});"="" causing a Javascript parsing error. This seems to be a bug

When using CHtml::tag it works even without setting data. This is interesting. But using CHtml::tag is more “Yii’ish” anyway, so you should use this code:




<?php echo CHtml::tag('div',array(

    'onClick'=>CHtml::ajax(array(

        'id'=>'country_list',

        'type'=>'POST',

        'url'=>$this->createUrl('sell/loadGame'),

        'success'=>'function(data) {}')

        )),

    'TEST');

?>