Yii Framework Forum: Ajax Checkbox With Two Different Url - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Ajax Checkbox With Two Different Url Rate Topic: -----

#1 User is offline   Carlos Adrián 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 30-August 13

Posted 16 October 2013 - 04:48 PM

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
0

#2 User is offline   Carlos Adrián 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 30-August 13

Posted 18 October 2013 - 06:50 AM

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
0

#3 User is offline   chandran 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 370
  • Joined: 08-October 12
  • Location:Austria

Posted 18 October 2013 - 03:23 PM

View PostCarlos Adrián, on 16 October 2013 - 04:48 PM, said:

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


Hi,

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



This can work..but not tested
Thanks
chandran nepolean

My own extension:
http://www.yiiframew...sion/ejqueryte/

My wiki
http://www.yiiframew...checkboxcolumn/

If it work dont hesitate to click +1 button
0

#4 User is offline   Carlos Adrián 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 30-August 13

Posted 18 October 2013 - 06:53 PM

View Postchandran, on 18 October 2013 - 03:23 PM, said:

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.
0

#5 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 19 October 2013 - 02:10 AM

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);
?>

1

#6 User is offline   Carlos Adrián 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 30-August 13

Posted 19 October 2013 - 04:52 AM

View Postandy_s, on 19 October 2013 - 02:10 AM, said:

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);
?>

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users