Yii Framework Forum: how to retrieve multiple checkbox value - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

how to retrieve multiple checkbox value Rate Topic: -----

#1 User is offline   vikram 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 60
  • Joined: 24-June 09

Posted 25 June 2009 - 07:08 AM

i have been using multiple checkbox in a page........if and i created all the checkbox in the same name like
<?php
$i=0;
echo CHtml::activeCheckBox($form,'select',array('name'=>'sel['.$i.']','va'=>$vaca->vacationtype));?>
$i=$i+1; ?>how do i retrieve value from it when multiple checkbox have been checked...................
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,871
  • Joined: 04-October 08
  • Location:DC, USA

Posted 25 June 2009 - 09:16 AM

$form->select should give you the selected values.
0

#3 User is offline   vikram 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 60
  • Joined: 24-June 09

Posted 26 June 2009 - 01:19 AM

no i cant get the values when i use $form->select..........plz do help me someone
0

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,871
  • Joined: 04-October 08
  • Location:DC, USA

Posted 26 June 2009 - 03:17 PM

I think what you need is CHtml::activeCheckBoxList() which will generate the list of checkboxes for you.
0

#5 User is offline   vikram 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 60
  • Joined: 24-June 09

Posted 26 June 2009 - 11:44 PM

no i am using pagination for displaying ten records per page and each record has to be given a check box and so i created in the above decribed way....tell me the way to create and retrieve value when the checkbox gets clicked in a better way .............someone do help me
0

#6 User is offline   dimis283 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 277
  • Joined: 24-January 09

Posted 27 June 2009 - 02:07 AM

You want to take the values at the post script?
I Have done someting simiral with plain php ant input text
<input type='text' name='ip[0]'>
<input type='text' name='ip[1]'>

At the post the
$_post['ip']
is an array of the values.
At javascript maybe a loop at boxes and see if they are selected ?
0

#7 User is offline   vikram 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 60
  • Joined: 24-June 09

Posted 27 June 2009 - 02:11 AM

k i agree with see above i can able to identify which checkbox has been clicked but i was not able to retrieve the "va" which i have used............
CHtml::activeCheckBox($form,'select',array('name'=>'sel['.$i.']','va'=>$vaca->vacationtype))
someone help me
0

#8 User is offline   dimis283 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 277
  • Joined: 24-January 09

Posted 27 June 2009 - 03:02 AM

So you want to take the value with  javascript?
I wrote this
echo CHtml::activeCheckBox($model,'id',array('name'=>'sel[0]',
'va'=>'1','onclick'=>'doing(this.id)'))

As Yii supports Jquery
<script>function doing(id)
    {alert($("#"+id+"").attr('va'))}
</script>

There is a message at netbeans that attr('va') is not supported at IE 5.5  ???, why you do not set the 'value' ?
alert($("#"+id+"").val()
0

#9 User is offline   vikram 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 60
  • Joined: 24-June 09

Posted 27 June 2009 - 03:19 AM

i forget to mention athing .............i.e checkbox should be checked ...any no of check box can be checked and when i click a submit button i need the values of the checkboxes...........it will be better if u suggest me a way for retrieving multiple values form the check box...........do help me plz
0

#10 User is offline   dimis283 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 277
  • Joined: 24-January 09

Posted 27 June 2009 - 03:51 AM

All this with javascript?
So you may see http://stackoverflow...ments-in-jquery
0

#11 User is offline   vikram 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 60
  • Joined: 24-June 09

Posted 27 June 2009 - 07:19 AM

i cant get u could u plz explain me further
0

#12 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,871
  • Joined: 04-October 08
  • Location:DC, USA

Posted 27 June 2009 - 11:55 AM

Please refer to http://www.yiiframew...uide/form.table
0

#13 User is offline   vikram 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 60
  • Joined: 24-June 09

Posted 27 June 2009 - 11:51 PM

i used the method that u have notified but its not working
0

#14 User is offline   amc 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 13-March 09

Posted 30 June 2009 - 08:12 AM

Vikramsundar88

If you need to "remember" a list of checkboxes which are ticked across a collection of pages in a paginated list without using form posts then the following works for me.

It uses the prototypejs library but no doubt it could be modified to use jquery. it works by using cookies to remember which checkboxes are ticked on each page.

download the attached cookies.js file and install a recent version of prototype http://www.prototypejs.org/

To include relevant js scripts in the page use following

<script src="<?php echo Yii::app()->request->baseUrl; ?>/js/prototype.js" type="text/javascript"></script>
<script src="<?php echo Yii::app()->request->baseUrl; ?>/js/cookies.js" type="text/javascript"></script>


Assuming you have a page containing a paginated list add a checkbox to the table with the name 'rememberMe'. Here I want to remember the id of the model entity which is checked so I assign the id of the model as the checkbox id


<?php foreach($xxxList as $n=>$model): ?>
        ......
      <input class='rememberMe' id='<?php echo $model->id; ?>' type='checkbox'  />
      .......
<?php endforeach; ?>


To retrieve the list of checked ids or reset (uncheck) the list I use the following form element at the bottom of the page containing a hidden field which will contain the list of ids submitted by the form. The exportIDs() function (contained in cookies.js) assigns the ids of the checked boxes to the hidden field.


<form method='post' action='xxx' name='xxx'>
  <input id='ids' name='ids' type='hidden' />
  <input type="submit" value="List" onclick="$('ids').value = exportIDs();"/>
  <input type="reset" value="Clear" onclick="eraseAllCookies(); reloadPage();"/>
</form>


Then in the controller action handling this post you can extract the list of ids submitted in the post into an array as follows


$ids = explode(',', $_POST['ids']);


Hope this makes sense. If anyone has a better solution could they let me know.


0

#15 User is offline   matkaz 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 10-August 09

Posted 18 August 2010 - 02:19 PM

Returning to the original question

View Postvikram, on 25 June 2009 - 07:08 AM, said:

how do i retrieve value from it when multiple checkbox have been checked ?


I suggest http://www.yiiframew...oc/cookbook/81/
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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