foreach and radiobuttonlist problem

Hello. I have encountered a bit of a problem with foreach and radiobuttonlist/ajax. I’m not actually sure where the problem is coming from.




<table>

<tr><td></td><td>No Access</td><td>View</td><td>Control</td><td>Admin</td></tr>

<?php foreach (CHtml::listData(HvVm::model()->with('type')->findAll("type.name='hv'"), 'id', 'name') as $id => $name): ?>

<tr>

<td><?php echo CHtml::link(CHtml::encode($name), array('HvVm/view', 'id' => $id)); ?></td>

<td><?php echo CHtml::radioButtonList('createpermission', 

                                      '', 

                                      array("none" => "", "view" => "", "control" => "", "admin" => ""),

                                      array('id' => uniqid(), 

                                            'separator' => "</td><td>",  

                                            'ajax' => array('type' => 'POST', 

                                                            'url' => array('UserGroup/createPermission',

                                                            'subject_id' => $model->id, 

                                                            'object_id' => $id), 

                                             "success"=>"function(data){ if (data) { $('#_users_permissions').html(data); } }"))); ?></td>

</tr>

<?php endforeach; ?>






public function actionCreatePermission($subject_id, $object_id)

  {

    $model = new Permission;

    printf("%s %s %s",$_POST['createpermission'], $subject_id, $object_id);

...



My problem is that the object_id being printed by the function actionCreatePermission is always the same. It is the last one in the list. I can’t imagine why this is. I know $id is being changed correctly because the CHtml::link works. But on the same line the ajax call passes the wrong object_id.

Any ideas?

You should try something like this




...

'ajax' => array(

  'type' => 'POST', 

  'url' => array('UserGroup/createPermission',

  'data' => array(

    'subject_id' => $model->id, 

    'object_id' => $id), 

  )

  ...



/Tommy

Oh man, really stupid mistake. I had the same radiobuttonlist ‘name’ for every row.

I actually fell asleep thinking about the problem and when I woke up I realized what I needed to do.

Fixed now.