codeigniter flashdata未按预期工作

yacmzcpb  于 4个月前  发布在  其他
关注(0)|答案(8)|浏览(60)

我在控制器中设置了flashdata,

public function customer() {
    $data['title'] = 'Black List Management';

    if ($this->input->post('single_black')) {
        //echo 'here';return;
        $wallet = trim($this->input->post('single_wallet', TRUE));
        $reason = trim($this->input->post('reason', TRUE));
        $match = preg_match('/^01[15-9]\d{8}$/', $wallet);

       //if not valid mobile
        if ($match == 0 || !$match) {

            $this->session->set_flashdata('message', 'The wallet is not valid Mobile no.');
            redirect('blacklist/index');
        }
        $is_blacklisted = $this->db->where('wallet', $wallet)->where('is_blacklisted', 1)->get('customers')->num_rows();

       //if already blacklisted
        if ($is_blacklisted > 0) {

            $this->session->set_flashdata('message', 'This wallet is already in blacklist');
            redirect('blacklist/index');
        }

        $this->form_validation->set_rules('reason', 'Reason', 'required');
        if ($this->form_validation->run() == FALSE) {// if invalid form
            $this->nuts_lib->view_loader('user', 'blacklist', $data);
            return;
        } else {
            $user_id = (int) $this->session->user_id;

            $query = $this->db->where('wallet', $wallet)->where('is_blacklisted', 0)->get('customers');
            $result = $query->result_array();
            if (count($result) > 0) {// if exist uppdate
                $customer_id = (int) $result[0]['id'];
                $blacklist = array(
                    'is_blacklisted' => 1,
                    'blacklist_meta' => date('Y-m-d H:i:s') . '|' . $user_id . '|' . $reason
                );
                $this->db->where('id', $customer_id)->update('customers', $blacklist);
            } else {// insert
                $new_blacklist = array(
                    'wallet' => $wallet,
                    'is_blacklisted' => 1,
                    'blacklist_meta' => date('Y-m-d H:i:s') . '|' . $user_id . '|' . $reason
                );
                $this->db->insert('customers', $new_blacklist);
            }
            $this->session->set_flashdata('message', 'Successfully Blacklisted');
            redirect('blacklist');
        }
    }
}

字符串
错误时从此customer方法重定向到以下索引方法

public function index() {
    $data['title'] = 'Black List Management';

    $this->nuts_lib->view_loader('user', 'blacklist', $data);
}


在我的视图文件(user/blacklist.php)中

$message = $this->session->flashdata('message');
if (isset($message)) {
     echo '<div class="alert alert-info">' . $message . '</div>';
}


因此,当得到$error它显示flashdata很好,但问题是当得到同样的错误下一次(提交表单后),然后flashdata不再显示。
目前为止我尝试的是CodeIgniter flashdata not working after redirect
我需要显示flashdata消息evry时间当得到$error

cygmwpex

cygmwpex1#

经过长时间的努力,它终于工作了。
这是我的解决方案(查看文件)

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

    ?>

字符串
之后,在我的控制器construct函数

public function __construct() {
    parent::__construct();
    .....
    $this->session->keep_flashdata('message');
}


它在每个错误中工作。仍然有一些愚蠢的问题,但到目前为止工作得很好

zzlelutf

zzlelutf2#

试试这个

$message = $this->session->flashdata('message');
if (isset($message)) {
 echo '<div class="alert alert-info">' . $message . '</div>';
 $this->session->unset_userdata('message');
}

字符串

qaxu7uf2

qaxu7uf23#

在您的email发送控制器中添加此构造函数:

public function __construct() {
        parent::__construct();
        $this->session->keep_flashdata('message');
    }

字符串

ibps3vxo

ibps3vxo4#

你面临的问题是一个相当简单的一个.它只会显示第一次,因为它不会检查第二次,如果错误进来.背后的原因是你把闪存数据在IF ..ELSE语句..这将只工作一次.
试试这个:

<?php if($this->session->flashdata('message')):?>
       <div class="alert alert-success" role="alert">
            <?php echo $this->session->flashdata('message');?> 
       </div>
<?php endif ?>

字符串
我想这对你有用...

332nm8kg

332nm8kg5#

而不是让闪光灯的消息在视图中,你可以让它进入您的控制器,并通过它到您的看法

控制器

<?php
    if($error){
        $this->session->set_flashdata('message','number is not valid');
        redirect('blacklist/index');
    }

    function index()
    {

    $error = $this->session->flashdata('message');// get you flash message

    $data = array();// create array

    //...

    $data['message'] = $error;// pass you message to array
    $this->load->view('someview',$data);// pass your message to your view
    }
    ?>

字符串

查看

// And in your view file
<?php if($message): 
echo '<div class="alert alert-info">' . $message . '</div>';
 endif; ?>

qc6wkl3g

qc6wkl3g6#

我在CI中构建的大多数项目中都是这样做的,这就是我如何做的(HTML标记是基础的);
控制器:

$this->session->set_flashdata('error', 'Invalid registration number');
redirect();

字符串
观点:

if ($error = $this->session->flashdata('error')) {
    echo '<div data-alert class="alert-box alert">';
        if (is_array($error))
        {
            foreach($error as $e)
            {
                echo '<p class="error">' . $e . '</p>';
            }
        }
        else
        {
            echo $error;
        }
        echo '<a href="#" class="close">&times;</a>';
    echo '</div>';
}


希望这能帮上忙。

lrpiutwd

lrpiutwd7#

这是你如何在CI中设置Flash消息

$this->session->set_flashdata('message','number is not valid');
redirect('blacklist/index');  //Redirect after setting flash message

字符串
重定向后在视图页中创建DIV

<div class="confirm-div alert alert-info"></div>


</body >之前添加此内容

<script>
    // assumes you're using jQuery
    $(document).ready(function() {
    $('.confirm-div').hide();
    <?php if($this->session->flashdata('message')){ ?>
    $('.confirm-div').html('<?php echo $this->session->flashdata('message'); ?>').show();
    <?php } ?>
    });

</script>

vaqhlq81

vaqhlq818#

在CI 3.1.11中遇到了相同的问题,已升级到CI 3.1.13,运行良好。

相关问题