Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Change User Active/Suspend Status on dropdown change in cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.14k
    Comment on it

    Hello Reader's!,If you want to change User Active / inactive status on click or dropdown change,Then I wrote this blog for you.

    first you need to create dropdown box which contain user status 0 for Suspend 1 for active.
    Example:

    <?php $options = array('1' => 'Activate', '0' => 'Suspend');
    echo $this->Form->select('user_status', $options, array('empty'=>false, 'name'=>'data[User][user_status]', 'default'=>$det['User']['user_status'], 'class'=>'ddStatus', 'data-user_id'=>$det['User']['id']));?>
    
    //(user_status) This is a field in database
    //Assign name and set the class with user is (array(.Condition.))

    //Then create Ajax function which contain user status nd send to controller for updation
    //Put this code before </body> tag

     

    <script type="text/javascript">	 
    		$(document).on('change', '.ddStatus', function(e) {
    			var element = $(this); 
    			currentStatus = $(this).val(); //User Current Status
    			$.ajax({
    				type: 'POST',
    				url: '<?php echo Router::url(array('controller'=>'users', 'action'=>'change_user_status'))?>', //change_user_status is a Function in controller
    				data: 'user_id='+element.data('user_id')+'&status='+currentStatus,
    				success: function(jsonResponse) {
    					var response = $.parseJSON(jsonResponse);
    					if(response.status != 'success')
    					{
    						element.val(!currentStatus);
    					}
    					
    					alert(response.message);
    					}
    				});
    			});			
      </script> 

    // After done ajax code now create function (change_user_status) in controller

     

    public function change_user_status()
    	{
    		if(!$this->request->is('AJAX'))
    		{
    			return $this->redirect(array('controller'=>'users', 'action'=>'home'));
    		}
    		
    		if(!$this->User->save(array('id'=>$this->request->data['user_id'], 'user_status'=>$this->request->data['status'])))
    		{
    			echo json_encode(array('status'=>'failure', 'message'=>'Unable to update status at the moment.'));
    			die;
    		}
    		
    		echo json_encode(array('status'=>'success', 'message'=>'User status updated.'));
    		die;
    	}

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: