如何使用codeigniter显示成功消息?

时间:2014-09-21 11:45:53

标签: php html codeigniter

我有一个来自布局,当我按下提交按钮时,此页面中的数据保存到数据库中。我可以在同一页面打印我的表单验证错误,但我无法将此成功消息打印到此页面,我可以将成功消息打印到另一页面,如何将消息显示到同一页面PLZ帮助。

这是控制器:

    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);

        }           
    }

这是我的模特:

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

以下是我的观点:

    <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>

3 个答案:

答案 0 :(得分:5)

插入后只需添加flash data(一次会话使用):

} 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');
}  

然后在视图(addRoom)中:

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

答案 1 :(得分:0)

使用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

使用查询调用 致电

$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>';
}
?>

答案 2 :(得分:0)

获取Flash数据后,可以使用 Codeigniter 中的自动刷新页面的以下功能进行重定向:

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