Xubayer pantho
Xubayer pantho

Reputation: 329

How to show success message using codeigniter?

i have a from layout, when i press submit button the data from this page save into database. I can print my form validation errors in the same page but i cannot print my success message into this page, i can print success message into another page, how can i show the message into same page plz help.

Here is controller:

    function addRoom(){
        $data['title'] = 'Add Room';
        $data['main_content'] = 'config/addRoom';
        $data['roomlists'] = $this->config_mdl->get_room_info();
        $this->load->view('_base/layout', $data);
    }

    function roomAdd(){
        $this->form_validation->set_rules('roomType', 'Room Type', 'trim|required|xss_clean');
        $this->form_validation->set_rules('roomName', 'Room Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('roomDetails', 'Room Details', 'trim|required|xss_clean');
        $this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">', '</div>');

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

            $this->addRoom();

        } else {
            $roomdata = array('room_type' => $this->input->post('roomType'),
                             'room_name' => $this->input->post('roomName'),
                             'room_details' => $this->input->post('roomDetails'));
            $this->config_mdl->roomAdd($roomdata);

        }           
    }

Here is my model:

    function roomAdd($roomdata)
    {
        return $this->db->insert('tbl_room_info', $roomdata);
    }

Here is My view:

    <div class="panel-body">
    <?php echo validation_errors(); ?>
    <?php $attributes = array('class' => 'form-horizontal', 'role' => 'form');
                        echo form_open_multipart('config/roomAdd', $attributes);
                    ?>
    <div class="form-group">
        <label for="roomType" class="col-sm-2 control-label">Room type</label>
        <div class="col-sm-2">
        <select name="roomType" class="form-control">
        <option>Select</option>
        <option value="1">1</option>
        <option value="2">2</option>
        </select>
        </div>
    </div>
    <div class="form-group">
        <label for="roomName" class="col-sm-2 control-label">Room Name</label>
        <div class="col-sm-6">
        <input type="text" class="form-control" name="roomName" placeholder="Type Room Name" />
        </div>
    </div>
    <div class="form-group">
        <label for="roomDetails" class="col-sm-2 control-label">Room Details</label>
        <div class="col-sm-6">
        <textarea class="form-control" name="roomDetails" rows="3"></textarea>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-6">
        <button type="submit" class="btn btn-primary">Submit</button>
        </div>
    </div>
    <?php echo form_close(); ?>
    </div>

Upvotes: 2

Views: 27613

Answers (3)

Pratap Tomar
Pratap Tomar

Reputation: 21

use cdn js

https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js
css 
https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.css

call with query call

$this->session->set_flashdata('error','Crediancial is wrong');



<?php
if($this->session->flashdata('notice') != ''){
echo '<script>toastr.warning("'.$this->session->flashdata('notice').'","Notice");</script>';
}

if($this->session->flashdata('error') != ''){
echo '<script>toastr.error("'.$this->session->flashdata('notice').'","Error");</script>';
}

if($this->session->flashdata('success') != ''){
echo '<script>toastr.success("'.$this->session->flashdata('success').'","Success");</script>';
}
?>

Upvotes: 2

Sukhdev Pawar
Sukhdev Pawar

Reputation: 11

After getting flash data, for redirect we can use below function for auto refresh page in Codeigniter:

header('refresh:3; url=' . base_url().'/admin_folder/adminController');

Upvotes: 0

Kevin
Kevin

Reputation: 41893

Just add a flash data (one time session usage) after insertion:

} else {
    $roomdata = array(
        'room_type' => $this->input->post('roomType'),
        'room_name' => $this->input->post('roomName'),
       'room_details' => $this->input->post('roomDetails')
    );

    $this->config_mdl->roomAdd($roomdata);

    // set flash data
    $this->session->set_flashdata('msg', 'Room added');
    redirect('controller_name/addRoom');
}  

Then in the view (addRoom):

<?php if($this->session->flashdata('msg')): ?>
    <p><?php echo $this->session->flashdata('msg'); ?></p>
<?php endif; ?>

Upvotes: 9

Related Questions