user4419336
user4419336

Reputation:

Codeigniter session flashdata not showing correct message

When I submit my form it does not display the correct flashdata message.

If my user group has been delete it should display the info message But for some reason it displays the delete_error_message even though has been success fully removed from database table.

Question: What is the best way of setting the flashdata message so it shows $this->session->set_flashdata('info', 'You have modified user groups!'); if been successfull and if not success full $this->session->set_flashdata('delete_error_message', 'Could not delete User Group');

<?php

class User_group_delete extends MX_Controller {

    public function __construct() {
        parent::__construct();

        $admin_userdata = $this->session->userdata('admin');

        if ($admin_userdata['token'] == FALSE) {

            $this->session->set_flashdata('token_message', 'Invalid Session Token Please Login Again');
            redirect('admin');

        } elseif ($admin_userdata['token'] == TRUE) {

            if (!$this->user->hasPermission('access', 'user_group/user_group_delete')) {
                redirect('admin/error/permission');
            }
        }

    }

    public function index() {

        $data['user_group_id'] = $this->uri->segment(4);

        $this->form_validation->set_rules('name', 'User Group Name', 'required|callback_validate');

        if ($this->form_validation->run($this) == FALSE) {

            $this->load->view('template/user_group/user_group_delete_view', $data);

        } else {


            if ($this->delete_user_group()) {

                $this->session->set_flashdata('info', 'You have modified user groups!');

                redirect('admin/user_group/user_group_list');

            } else {

                $this->session->set_flashdata('delete_error_message', 'Could not delete User Group');

                redirect('admin/user_group/user_group_list');

            }

        }
    }

    public function delete_user_group() {
        $this->db->where('name', $this->input->post('name'));
        $this->db->where('user_group_id', $this->uri->segment(4));
        $this->db->delete($this->db->dbprefix .'user_group');
    }

    public function validate() {
        if ($this->user->hasPermission('modify', "user_group/user_group_delete")) {

            return TRUE;

        } else {

            $this->form_validation->set_message('validate', 'Warning: You do not have permission to modify user groups');

            return FALSE;

        }
    }
}

View Messages

<?php if ($this->session->flashdata('success')) { ?>
<div class="alert alert-info text-center">
<?php echo $this->session->flashdata('success'); ?>
</div>
<?php } ?>

<?php if ($this->session->flashdata('info')) { ?>
<div class="alert alert-info text-center">
<?php echo $this->session->flashdata('info'); ?>
</div>
<?php } ?>

<?php if ($this->session->flashdata('delete_error_message')) { ?>
<div class="alert alert-danger">
<?php echo $this->session->flashdata('delete_error_message'); ?>
</div>
<?php } ?>

Upvotes: 0

Views: 1189

Answers (2)

Yashah1990
Yashah1990

Reputation: 66

I think delete_user_group() is not returning any value therefore it's not working. You can try this below code :

public function delete_user_group() {
    $this->db->where('name', $this->input->post('name'));
    $this->db->where('user_group_id', $this->uri->segment(4));
    if($this->db->delete($this->db->dbprefix .'user_group'))
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

Upvotes: 1

Saty
Saty

Reputation: 22532

You forget return type in your delete_user_group() function It would be like below

public function delete_user_group() {
 $this->db->where('name', $this->input->post('name'));
 $this->db->where('user_group_id', $this->uri->segment(4));
 $this->db->delete($this->db->dbprefix .'user_group');
 $row=$this->db->affected_rows();
 if($row>0){
  return TRUE;
 }else{
  return FALSE;
 }
}

Upvotes: 2

Related Questions