Hi all,
having successfully created a dependent dropdownlist, coding with ajax/jquery under Yii last week, I’m now trying to do the same with a checkbox, but for the moment I’m not as much successful and some help would be greatly appreciated !
[b]
- What I’m trying to do :
[/b]
When I check/uncheck the checkbox, I would like the corresponding boolean database field to update and the checkbox to change its display state.
[b]
- My code :
[/b]
[indent]
- View : index.php
[/indent]
<div id="submenu">
<?php echo CHtml::link('Créer un utilisateur', array('users/create') ); ?>
</div>
<?php echo CHtml::form('', 'post', array('class'=>'formulaire') ); ?>
<div class="tickets-grid-header">
</div>
<table class="default-grid">
<thead>
<th><a href="#">Nom</a></th>
<th><a href="#">Email</a></th>
<th><a href="#">Rôle</a></th>
<th><a href="#">Actif</a></th>
</thead>
<?php if( !empty($users) && is_array( $users ) ): ?>
<?php foreach( $users as $u ): ?>
<tr>
<td>
<?php echo CHtml::link($u['name'] . ' '. $u['lastname'], array('users/update', 'user_id' => $u['user_id'] ) ); ?>
</td>
<td>
<?php echo $u['email']; ?>
</td>
<td>
<?php echo $u['itemname']; ?>
</td>
<td>
<?php echo CHtml::checkBox('UserEst_valide', $u['est_valide'],
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('users/updateUserEst_valide')
))); ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</table>
</form>
<div class="tickets-grid-footer"></div>
[indent]
- Controller : UsersController.php
[/indent]
<?php
class UsersController extends CRootController {
public function actionUpdateUserEst_valide()
{
$query = "UPDATE users SET est_valide=:est_valide WHERE user_id=:user_id";
$command = Yii::app()->db->createCommand($query);
$command->bindValue(':est_valide', $_POST['User']['est_valide'], PDO::PARAM_INT );
$command->bindValue(':user_id', $u['user_id'], PDO::PARAM_INT );
$command->execute();
}
[b]
- What the issue is :
[/b]
[indent]1- the database doesn’t update[/indent]
[indent]2- the checkbox remains in its original display state (checked or unchecked) when I click on it[/indent]
So thanks in advance to anyone who could help me !
PS : I’m a noob in ajax/jquery/Yii and I got the original code (without my ajax implementation) from an existing project.