Vue : admin_add.ctp
<div class="page-header">
<h1>Ajouter un membre</h1>
</div>
<?php
echo $this->Form->create('User', array('type' => 'file'));
echo $this->Form->input('picture', array('label' => 'Photo', 'type' => 'file'));
echo $this->Form->input('username', array('label' => 'Le login:',
'style' => 'height: 25px;'));
echo $this->Form->input('name',array('label' => 'Nom:',
'style' => 'height: 25px;'));
echo $this->Form->input('password',array('label' => 'Mot de passe: ',
'style' => 'height: 25px;'));
echo $this->Form->input('passwordconfirm',array('label' => 'Confirmer le mot de passe: ', 'type' => 'password',
'style' => 'height: 25px;'));
echo $this->Form->input('role', array('value' => 'membre', 'type' => 'hidden'));
echo $this->Form->input('level_id', array('label' => 'Le niveau scolaire: ',
'style' => 'height: 25px;',
'class' => 'ajaxListe',
'data-url' => $this->Html->url( array( 'controller' => 'users', 'action' => 'getgroupes', 'admin' => true ), true )
));
echo $this->Form->input('groupe', array('options' => array(),
'style' => 'height: 25px; width: 50px;'));
echo $this->Form->input('actif', array('label' => 'Compte actif'));
echo '<br />';
echo $this->Form->end(array('label' => 'Ajouter l\'utilisateur', 'class' => 'btn btn-primary'));
?>
Controller: UsersController
public function admin_getgroupes() {
if ( $this->request->is( 'ajax' ) ) {
$this->loadModel('Level');
$result = $this->Level->find( 'first', array(
'conditions' => array( 'id' => $_GET'id'] ) ,
'fields' => array( 'nbre_groupes' )
));
$ret'res'] = $result'Level']'nbre_groupes'];
echo json_encode( $ret );
exit();
}
}
Ajax (jQuery):
$(".ajaxListe").live("click", (function(e){
var select = $(this);
$.get(
select.data('url'),
{ id: $( '#UserLevelId' ).val() },
function( data ) {
var obj = jQuery.parseJSON( data );
$( '#UserGroupe' ).find('option').remove();
for(var i=0; i < obj.res; i++){
$( '#UserGroupe' ).append(new Option(i+1, i+1, false, false));
}
}
);
return false;
}));