如何使用 CodeIgniter 3 中的引导警报组件显示错误警报

问题描述 投票:0回答:2

我正在 Codeigniter 中创建上传、编辑和删除,我希望 bootstrap 警报应在 div 警报中显示上传错误(如果有)。控制器代码如下,请帮忙。

控制器代码从这里开始

<?php 
defined('BASEPATH') OR exit ('No direct script access allowed');

/**
 * 
 */
class Admin_Controller extends CI_Controller
{
    
    function __construct()
    {
        parent::__construct();
        $this->load->library("session");
    }


    public function registerAdminDetails()
    {
        $config['upload_path'] = 'assets/img/uploads/reg_Admin/';
        $config['allowed_types'] = 'jpg|jpeg|png';
        $config['max_size'] = '2048';

        $this->load->library('upload', $config);

        if(! $this->upload->do_upload('avatar'))
        {

            $error = array('error' => $this->upload->display_errors());
            $this->session->set_flashdata('error', $error);
         
            $this->load->view('admin_access/admin_add_new');
        }   
        else
        {
            $avatar_upload = $this->upload->data();

            $data = array(
            'admin_name' => $this->input->post('admin_name'),
            'admin_username' => $this->input->post('admin_username'),
            'admin_email' => $this->input->post('admin_email'),
            'admin_password' => $this->input->post('password'),
            'admin_profile_pic' => $this->input->post('avatar'),
            'admin_profile_pic' => $config['upload_path'].$avatar_upload['file_name']);

            $this->db->insert('admin_profile',$data);

            echo "<script>alert('New Admin Successfully Enrolled')</script>";
            $this->load->view('admin_access/profile/admin_add_new');
        }
        
    }
}
?>
codeigniter bootstrap-4
2个回答
0
投票
            $result = $this->add_model->save($fields,array('id' => $id));
          
            if($result)
            {
                $this->session->set_flashdata('message', array('message' => 'Updated Successfully','class' => 'alert alert-success fade in'));
            }
            else
            {
                $this->session->set_flashdata('message', array('message' => 'Something went wrong, please try again','class' => 'alert alert-danger fade in'));
            }

0
投票

控制器:

if($result <= 0) {
    $this->session->set_flashdata('error', 'There is error');
} else {
    $this->session->set_flashdata('success', 'Yey!');
}

我的示例视图(是的,我正在使用引导程序):

<div class="col-sm-12">
    <?php
        $this->load->helper('form');
        $error = $this->session->flashdata('error');
        if($error) {
    ?>
    <div class="alert alert-danger alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
        <?php echo $this->session->flashdata('error'); ?>                    
    </div>
    <?php } ?>
    <?php  
        $success = $this->session->flashdata('success');
        if($success)
        {
    ?>
    <div class="alert alert-success alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
        <?php echo $this->session->flashdata('success'); ?>
    </div>
    <?php } ?>
                            
    <div class="row">
        <div class="col-md-12">
            <?php echo validation_errors('<div class="alert alert-danger alert-dismissable">', ' <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button></div>'); ?>
        </div>
    </div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.