我正在开发codeigniter。我做了sql查询将数据插入到database.my查询运行完美,并显示在数据库表中。
但我希望在我的数据插入表格时显示消息(就像数据插入成功一样)。
这是我的控制器代码:
public function insert(){
$post=$this->input->post();
unset($post['submit']);
if($this->testimonial_model->insert_testimonial($post))
{
$this->session->set_flashdata('feedback',"Data inserted successfully.");
$this->session->set_flashdata('feedback_class','alert-success');
}else{
$this->session->set_flashdata('feedback',"failed to add, Please Try again");
$this->session->set_flashdata('feedback_class','alert-danger');
}
return redirect('testimonial_edit');
}
这是我的型号代码:
public function insert_testimonial($insert){
return $this->db->insert('testimonial',$insert);
}
这是我的视图代码,我的视图文件名是testimonial_edit
<?php
if($feedback=$this->session->flashdata('feedback')):
$feedback_class=$this->session->flashdata('feedback_class');
?>
<div class="row">
<div class="col-lg-6">
<div class="alert alert-dismissable <?php $feedback_class?>">
<?php $feedback?>
</div>
</div>
</div>
<?php
endif;
?>
在此代码下面,我的表单代码就在那里。
当我点击提交按钮填写表格后,数据已成功插入,但显示以下错误
未找到404页面 找不到您请求的页面。
和此错误页面的网址是
http://localhost/lalcoresidency/testimonials/dashbord
请帮我找出解决方案
答案 0 :(得分:3)
您无法重定向到视图文件,因此请尝试向控制器添加一个只加载视图的方法;然后重定向到该方法:
public function insert(){
$post=$this->input->post();
unset($post['submit']);
if($this->testimonial_model->insert_testimonial($post))
{
$this->session->set_flashdata('feedback',"Data inserted successfully.");
$this->session->set_flashdata('feedback_class','alert-success');
}else{
$this->session->set_flashdata('feedback',"failed to add, Please Try again");
$this->session->set_flashdata('feedback_class','alert-danger');
}
redirect('current_controller_name/testimonial_edit');
}
/* add this function that you can redirect to,
and which will load the view file of the same name */
function testimonial_edit(){
$this->load->view('testimonial_edit');
}
至于您的成功消息未显示,请尝试:
<?php if($this->session->flashdata('feedback')){ ?>
<div class="row">
<div class="col-lg-6">
<div class="alert alert-dismissable <?php echo $this->session->flashdata('feedback_class'); ?>">
<?php echo $this->session->flashdata('feedback'); ?>
</div>
</div>
</div>
<?php } ?>
答案 1 :(得分:0)
试一试: -
public function insert(){
$this->load->library('session');
$post=$this->input->post();
unset($post['submit']);
if($this->testimonial_model->insert_testimonial($post))
{
$this->session->set_flashdata('feedback',"Data inserted successfully.");
$this->session->set_flashdata('feedback_class','alert-success');
}else{
$this->session->set_flashdata('feedback',"failed to add, Please Try again");
$this->session->set_flashdata('feedback_class','alert-danger');
}
echo ($this->session->flashdata('feedback_class')) ? $this->session->flashdata('feedback_class') : '';
echo ($this->session->flashdata('feedback')) ? $this->session->flashdata('feedback') : '';
die;
redirect(base_url('testimonial_edit'));
}
答案 2 :(得分:0)
在配置文件帮助数组
中添加 url 帮助器喜欢$config['helper']=array("url");