flashdata在codeigniter中根本不起作用

时间:2018-11-16 13:00:40

标签: php codeigniter

大家好,我尝试过多种方法

$this->session->keep_flashdata();
$this->session->set_flashdata();

但是可以在重定向时加载消息,我将在下面发布我的代码,以帮助我解决此问题。

控制器:

        $params = array(
          'full_name' => $this->input->post('full_name'),
          'father_name' => $this->input->post('father_name'),
          'email' => $this->input->post('email'),
          'phone' => $this->input->post('phone'),
          'password' => $this->input->post('password'),
          'cnic_no' => $this->input->post('cnic_no'),
          'manager' => $this->input->post('manager'),
          'department_id' => $this->input->post('department_id'),
          'created_by' => $_SESSION['user_id'],
          'position' => $this->input->post('position'),
          'created_at' => $now,
          );
          $this->Maintainer_basic_model->insert($params);
          $this->session->set_flashdata('success', 'Maintainer added successfully');
          redirect('maintainer_basic/index');

查看:

<?php if($message = $this->session->flashdata('success')): ?>
  <div class="row">
    <div class="col-md-12 form-group">
        <div class="alert-success alert">
            <?php echo $message; ?>
        </div>
    </div>
  </div>
<?php endif ?>

1 个答案:

答案 0 :(得分:1)

根据您提供的代码,我认为您尚未将会话库加载到 config / autoload.php
检查以下代码以了解主意。

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

$autoload['packages'] = array();

$autoload['libraries'] = array("session","form_validation","database");

$autoload['drivers'] = array();

$autoload['helper'] = array('url','path','form');

$autoload['config'] = array();

$autoload['language'] = array();

$autoload['model'] = array();

在那之后,您应该在视图中检查打印会话值的功能。请按照以下代码来了解想法。由于 session 库直接构成了 session对象。因此您可以轻松地在 if()条件下对其进行检查。

<?php if($this->session->success): ?>
                <p class="alert alert-success" id="message"><?php echo $this->session->success; ?></p>
           <?php endif; ?>
            <?php if($this->session->error): ?>
                <p class="alert alert-warning" id="message"><?php echo $this->session->error; ?></p>
            <?php endif; ?>