如何在codeigniter中验证失败后保存动态添加的表单输入

时间:2016-08-01 07:38:55

标签: jquery html codeigniter

我的表单有问题。因此,当用户点击链接时,我有一些字段是generetad,如果验证失败,我按下表单提交按钮后会丢失这些字段。如果验证通过,则一切正确,数据将保存到数据库中。

所以这些是那些字段,也是添加新字段的脚本:

查看:

<tr>
      <td><input type="checkbox" name="chk"/></td>
     <!-- <td><input type="text" name="date[]" value="" /></td>-->
      <td><input type="text" name="work[]" value="" class="task" />
          <span class="text-danger"><?php echo form_error('work[]');?></span>
      </td>
      <td><input type="text" name="partner[]" value="<?php echo set_value('partner[]'); ?>" />
      </td>
      <td>
      <select name="director[]">
        <option value="" disabled selected>Select Your Director</option>
        <option value="hkn">HKN</option>
        <option value="anita">Anita</option>
        <option value="ravi">Ravi</option>
        <option value="ameesha">Ameesha</option>
      </select>
      <span class="text-danger"><?php echo form_error('director[]');?></span>
      </td>
      <td><input type="text" name="time[]" value="" />
          <span class="text-danger"><?php echo form_error('time[]');?></span>
      </td>
      <td>
          <textarea class="form-control" name="task[]" rows="2" id=""></textarea>
          <span class="text-danger"><?php echo form_error('task[]');?></span>
      </td>
      <td><textarea class="form-control" name="status[]" rows="2" id=""></textarea>
          <span class="text-danger"><?php echo form_error('status[]');?></span>
      </td>
  </tr>

控制器:

function add_task(){
        /* Checking the all validation of task form*/
        //$this->form_validation->set_rules('date', 'Date', 'required');
        $this->form_validation->set_rules('work[]', 'Types of Work', 'required');
        //$this->form_validation->set_rules('partner[]', 'Worked With', 'required');
        $this->form_validation->set_rules('director[]', 'Director', 'required');
        $this->form_validation->set_rules('time[]', 'No Of Hours', 'required');
        $this->form_validation->set_rules('task[]', 'Task Details', 'required');
        $this->form_validation->set_rules('status[]', 'Task Status', 'required');

        if ($this->form_validation->run()) {
            /* Taking the data from form*/
            $todayDate = date('Y-m-d');
            $work=$this->input->post('work');
            $partner=$this->input->post('partner');
            $director=$this->input->post('director');
            $time=$this->input->post('time');
            $task=$this->input->post('task');
            $status=$this->input->post('status');

            $count=count($this->input->post('work'));
            $data =array();
            for($i=0; $i<$count; $i++) {
            $data[$i] = array(
                'name'       =>$this->session->userdata('emp_name'),
                'date'       =>$todayDate,
                'work'       =>$work[$i],
                'partner'    =>$partner[$i],
                'director'   =>$director[$i],
                'time'       =>$time[$i],
                'task'       =>$task[$i],
                'status'     =>$status[$i]
                );
                }
                $add=$this->digital_hodm_model->add_task($data);
                /* Display Success message if data inserted successfully in database*/

                    if($add){
                        $this->session->set_flashdata('hodm_form',"All HODM Data Inserted Successfully.");
                        $this->session->set_flashdata('hodm_form_class','alert-success');
                    }else{
                        /* Displaying the error message*/
                        $this->session->set_flashdata('hodm_form',"failed to add, Please Try again");
                        $this->session->set_flashdata('hodm_form_class','alert-danger');
                    }
                return redirect('home');
        } else {
            $this->load->view('public/digital_hodm_view');
        }
    }

所以我的问题是:在验证失败后可以保存动态生成的表单字段及其值吗?

谢谢

0 个答案:

没有答案