我已经编辑了以下代码,以便及时更新我所做的更改。我已设法将其保存但我仍然无法在重定向完成时显示“成功”消息。此外,如果出现错误错误,如何保存输入的数据?
控制器
class Addsale extends CI_Controller {
function __construct(){
parent::__construct();
}
function index() {
if(!$this->session->userdata('logged_in')) {
redirect('admin/home');
}
// set data
$data['title'] = "Add Sale";
$data['sales_pages'] = $this->sales_model->getSalesPages();
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('location', 'Location', 'trim|required');
$this->form_validation->set_rules('bedrooms', 'Bedrooms', 'trim|is_natural|required');
$this->form_validation->set_rules('bathrooms', 'Bathrooms', 'trim|required');
$this->form_validation->set_rules('condition', 'Condition', 'trim|required');
$this->form_validation->set_rules('description', 'Description', 'trim|required');
$this->form_validation->set_rules('price', 'Price', 'trim|required');
if($this->form_validation->run() === TRUE) {
$data = array(
'name' => $this->input->post('name', TRUE),
'location' => $this->input->post('location', TRUE),
'bedrooms' => $this->input->post('bedrooms', TRUE),
'bathrooms' => $this->input->post('bathrooms', TRUE),
'condition' => $this->input->post('condition', TRUE),
'description' => $this->input->post('description', TRUE),
'price' => $this->input->post('price', TRUE)
);
$this->sales_model->addSale($data);
redirect('admin/addsale' , $data);
$this->session->set_flashdata('success', 'Page Saved');
}else{
$data['content'] = $this->load->view('admin/addsale', NULL, TRUE);
$this->load->view('template', $data);
}
}
}
查看
<?php
//Setting form attributes
$formAddSale = array('id' => 'addSale', 'name' => 'addSale');
$saleName = array('id' => 'name', 'name' => 'name');
$saleLocation = array('id' => 'location', 'name' => 'location');
$saleBedrooms = array('id' => 'bedrooms','name' => 'bedrooms');
$saleBathrooms = array('id' => 'bathrooms','name' => 'bathrooms');
$saleCondition = array('id' => 'condition','name' => 'condition');
$saleDescription = array('id' => 'description','name' => 'description');
$salePrice = array('id' => 'price','name' => 'price');
?>
<section id = "validation"><?php print $this->session->flashdata('success');?> </section>
<section id = "validation"><?php echo validation_errors();?></section>
<?php
echo form_open('admin/addsale/', $formAddSale);
echo form_fieldset();
echo form_label('Name:', 'name');
echo form_input($saleName);
echo form_label ('Location', 'location');
echo form_input($saleLocation);
echo form_label ('Bedrooms', 'bedrooms');
echo form_input($saleBedrooms);
echo form_label ('Bathrooms', 'bathrooms');
echo form_input($saleBathrooms);
echo form_label ('Condition', 'condition');
echo form_input($saleCondition);
echo form_label ('Price', 'price');
echo form_input($salePrice);
echo form_label ('Description', 'description');
echo form_textarea($saleDescription);
echo form_submit('submit','Submit');
echo form_fieldset_close();
echo form_close();
?>
模型
function addSale($data) {
$this->db->insert('sales', $data);
return;
}
答案 0 :(得分:3)
您是否正在使用set_message来显示“已完成”的消息?如果是这样,那么就不会显示,因为你正在进行重定向。
尝试在重定向之前在flashdata中设置一条消息,如下所示:
$这 - &GT;会话而&GT; set_flashdata( '成功', '页面已保存');
然后在您的视图中将其打印出来:
打印$ this-&gt; session-&gt; flashdata('success')
答案 1 :(得分:0)
在我看来,这行中有太多“=”:
if($this->form_validation->run() === TRUE) {