Ajax Checkbox With Two Different Url

I have a checkBox and I use ajax in it. What I want to do is to use one ‘url’ parameter when I check and another different one when I uncheckit. For example:

When I check:


'url'=> CController::createUrl('/item/add'),

When I uncheck:


'url'=> CController::createUrl('/item/remove'),

Of course I do not want to check the state of the checkBox in the view from the controller.

What is the best approach for it?

Thank you

No answers? no suggestions? I will try with a button group, two buttons that toogle. Different buttons, different ‘url’ parameters. It should work.

Thank you anyway

Hi,

When I check and uncheck:


'url'=> CController::createUrl('/item/operation'),param=>array('operation'=>$this.selected?'add':deletion),

This can work…but not tested

It does not work for me. $this refers to the controller, and $this.selected does not exist.

I would do it this way:




<input type="checkbox" id="chk" />


<?php

$url1 = $this->createUrl(...);

$url2 = $this->createUrl(...);

$js = <<<JS

$('#chk').change(function(){

    $.post(this.checked ? "$url1" : "$url2", ...);

});

JS;

Yii::app()->clientScript->registerScript('chk', $js);

?>



Thank you!!! It works!!! My final code is:




<input type="checkbox" id="chk">

<?php 

	$url1 = $this->createUrl('/item/add');

	$url2 = $this->createUrl('/item/remove');

	$js = '

	$(\'#chk\').change(function(){

	    $.post(this.checked ? "'.$url1.'" : "'.$url2.'", function(data) {

	    	$("#items").html(data);

	    });

	});

	';

	Yii::app()->clientScript->registerScript('chk', $js);

?>