提交表格后的Codeigniter Flash数据?

时间:2013-06-17 20:10:34

标签: codeigniter flash

我对使用Codeigniter和Flash Data进行表单验证有疑问。我的表单位于URL字符串的深处,因此我认为在页面上显示错误消息的最佳方式是使用CI闪存数据。我很难搞清楚如何实际显示错误信息。我不确定我哪里出错了。我哪里错了?谢谢大家。

   public function form_validate_cars()
   {
     $this->load->library('form_validation');  

         $this->form_validation->set_rules("value", "<b>Value</b>", "trim|required|is_numeric|xss_clean");
         $this->form_validation->set_rules("exterior_color", "<b>Exterior Color</b>", "trim|required|xss_clean");
         $this->form_validation->set_rules("interior_color", "<b>Interior Color</b>", "trim|required|xss_clean");
         $this->form_validation->set_rules("mileage","<b>Mileage</b>","trim|required|is_numeric|xss_clean");

         if ($this->form_validation->run() == FALSE)
          {   
             // if person does not fill in correct info, they get resubmitted back to the form.
             // redirect('site/item/2332');
             // echo 'Hello World!';
             $msg = "Please fill out proper data";
             $this->session->set_flashdata('test', $msg);
             // echo 'Please fill out needed ifnormation!';
          } 
          else 
          {

            $this->load->model('model_data');
            $this->model_data->add_new_value();
            redirect('/site/thanks');

          }

   }

我的观点

   echo form_open('site/form_validate_cars');

   print_r($this->session->flashdata('test'));

   echo validation_errors();

   my form continues below ....... 

2 个答案:

答案 0 :(得分:1)

$this->session->set_flashdata('test', $msg);使redirect成为所需网址之后。

查看

中的

if ($this->session->flashdata('result') != ''): 
    echo $this->session->flashdata('result'); 
endif;

没有会话的更好方法

public function form_validate_cars()
   {
     $this->load->library('form_validation');  

         $this->form_validation->set_rules("value", "<b>Value</b>", "trim|required|is_numeric|xss_clean");
         $this->form_validation->set_rules("exterior_color", "<b>Exterior Color</b>", "trim|required|xss_clean");
         $this->form_validation->set_rules("interior_color", "<b>Interior Color</b>", "trim|required|xss_clean");
         $this->form_validation->set_rules("mileage","<b>Mileage</b>","trim|required|is_numeric|xss_clean");

       if ($this->form_validation->run() == FALSE)
          {   
             // if person does not fill in correct info, they get resubmitted back to the form.
             // redirect('site/item/2332');
             // echo 'Hello World!';
             $data['error'] = "Please fill out proper data";
             $this->load->view('desired_view', $data);
          } else {
            $this->load->model('model_data');
            $this->model_data->add_new_value();
            redirect('/site/thanks');

          }

   }

并在视图

echo (isset($error)) ? $error : "";

答案 1 :(得分:0)

分配flashdata后,使用redirect()重定向回表单。然后这将获取flashdata,确保检查它是否已填满。