我正在使用 CodeIgniter 3,即使在重定向后,我在该项目上设置的闪存数据也不会被清除。刷新页面或再次访问页面后仍然显示。我该如何解决这个问题?
Controller 中的函数。
public function updateDonor($donor)
{
$birthDate = $this->input->post('donordob');
$currentDate = date("Y-m-d");
$age = date_diff(date_create($birthDate), date_create($currentDate));
$this->form_validation->set_rules('donorname', 'Donor Name', 'required');
$this->form_validation->set_rules('donornic', 'Donor NIC', 'required');
$this->form_validation->set_rules('donordob', 'Donor DOB', 'required');
$this->form_validation->set_rules('donorweight', 'Donor Weight', 'required');
$this->form_validation->set_rules('donormobile', 'Donor Mobile', 'required');
if ($this->form_validation->run()) {
$data = [
'DonorName' => $this->input->post('donorname'),
'DonorNIC' => $this->input->post('donornic'),
'DonorDOB' => $this->input->post('donordob'),
'DonorAge' => $age->format("%y"),
'DonorWeight' => $this->input->post('donorweight'),
'DonorMobile' => $this->input->post('donormobile'),
];
$this->load->model('Donor_Model');
$data['donor'] = $this->Donor_Model->updateDonor($data, $donor);
$this->session->set_flashdata('donorupdated', 'Donor detailed updated successfully!');
redirect(base_url('index.php/staff/viewdonors'));
} else {
$this->editDonors($donor);
}
}
View 中的代码
<?php if ($this->session->flashdata('donorupdated')) { ?>
<script>
alertify.set('notifier', 'position', 'top-right');
alertify.success("<?php echo $this->session->flashdata('donorupdated'); ?>")
</script>
<?php } ?>
最佳答案
只需添加 unset($_SESSION['donorupdated']);
<script>
之后标签如下所示
<?php if ($this->session->flashdata('donorupdated')) { ?>
<script>
alertify.set('notifier', 'position', 'top-right');
alertify.success("<?php echo $this->session->flashdata('donorupdated'); ?>")
</script>
<?php unset($_SESSION['donorupdated']); ?>
<?php } ?>
关于php - CodeIgniter 3 中重定向后 Flashdata 未清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70457291/