问题描述
大家好,对于如何清除 setflashdata?
有想法的人,我是 codeigniter Framework 的新手。
控制器:-
public function add(){
$this->load->view('layout/header');
$this->load->view('blog/add');
$this->load->view('layout/footer');
}
public function submit(){
$result = $this->m->submit();
if($result){
$this->session->set_flashdata('success_msg','Record added successfully');
}else{
$this->session->set_flashdata('error_msg','Faill to add record');
}
redirect(base_url('blog/index'));
}
<h3>Blog list</h3>
<?PHP
if($this->session->flashdata('success_msg')){
?>
<div class="alert alert-success alert-status">
<?PHP echo $this->session->flashdata('success_msg'); ?>
</div>
<?PHP
}
?>
<?PHP
if($this->session->flashdata('error_msg')){
?>
<div class="alert alert-success">
<?PHP echo $this->session->flashdata('error_msg'); ?>
</div>
<?PHP
}
?>
<a href="<?PHP echo base_url('blog/add'); ?>" class="btn btn-primary">Add New</a>
这是数据表视图
<table class="table table-bordered table-responsive">
<thead>
<tr>
<td>ID</td>
<th>Title</th>
<th>Description</th>
<th>Created at</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?PHP
if($blogs){
foreach($blogs as $blog){
?>
<tr>
<td><?PHP echo $blog->id; ?></td>
<td><?PHP echo $blog->title; ?></td>
<td><?PHP echo $blog->description; ?></td
</tr>
<?PHP
}
}
?>
</tbody>
</table>
解决方法
在您的视图中使用它:-
<?php
if($this->session->flashdata('success')){
?>
<div class="alert alert-success" style="display:none;">
<?php echo $this->session->flashdata('success'); ?>
<?php
} else if($this->session->flashdata('error')){
?>
<div class = "alert alert-danger" style="display:none;">
<?php echo $this->session->flashdata('error'); ?>
</div>
<?php } ?>
把它放在你的前 </body>
标签视图中:-
<script type="text/javascript" src="<?=base_url()?>assets/js/jquery.min.js" ></script>
<script type="text/javascript" rel="stylesheet">
$('document').ready(function(){
$(".alert").fadeIn(1000).fadeOut(5000);
});
</script>
,
这是一个 CodeIgniter 问题。
对于您的问题解决方案,请使用以下视图
查看:
<?php
if ($this->session->flashdata ( 'success' )) {
?>
<div class="alert alert-success">
<?php echo $this->session->flashdata('success'); ?>
<?php
$this->session->unset_userdata ( 'success' );
} else if ($this->session->flashdata ( 'error' )) {
?>
<div class="alert alert-danger">
<?php echo $this->session->flashdata('error'); ?>
</div>
<?php
$this->session->unset_userdata ( 'error' );
}
?>