在codeigniter中提交表单失败后无法保留值

时间:2017-05-25 09:40:01

标签: php forms codeigniter

我希望如果表单未正确提交,那么用户输入的值不会丢失。表单内置于codeigniter

查看

<?php echo form_open_multipart('user/add_data'); ?>
    <?php
        $data = array(
          'type'=>'text', 
          'name'=>'name', 
          'class'=>'form-control', 
          'required' => 'required',
          'value' => set_value('name')
          );
    ?>
    <?php echo form_input($data); ?>

    <?php
         $data = array(
            'type'=>'file',
            'name'=>'userfile',
            'class'=>'fileinput btn-info',
            'id'=>'filename3',
            'data-filename-placement'=>'inside',
            'style' => 'margin-left: 330px',
            'title'=>'If any document upload here (* XLS | DOC | PDF | DOCX | XLSX )'
         );

         echo form_upload($data); 
    ?> 

    <?php
        $data = array(
            'type'=>'submit',
            'class'=>'btn btn-primary pull-right',
            'name'=>'submit',
            'content'=>'Submit'
        );
        echo form_button($data); 
      ?>


<?php echo form_close(); ?>

控制器

public function add_requirement_data() {
    $config['upload_path'] = './request/';
    $config['allowed_types'] = 'xls|xlsx|doc|docx|pdf';
    $config['max_size'] = 9000000;
    $config['max_width'] = 1024;
    $config['max_height'] = 768;
    $config['encrypt_name'] = TRUE;

    $this->load->library('upload', $config);

    if (!$this->upload->do_upload('userfile')) 
      {
        $data = array('upload_data' => $this->upload->data());
        if ($data['upload_data']['file_size'] == '0') 
          {
            $this->session->set_flashdata('req_msg', 'Cannot Upload Empty File');
            redirect('user/requirement');
          } 
        else 
          {
            if ($this->um->create_requirement_nofile($instanthire_main_id)) 
              {
                $this->session->set_flashdata('req_msg', 'Requirment raised successfully');
                redirect('user/requirement');
              }
          }
      } 
}

任何人都可以告诉我们如何在codeigniter表单中保留值

1 个答案:

答案 0 :(得分:1)

使用会话
让我们考虑一下你的名字输入标签
在你看来是

<?php
        $data = array(
          'type'=>'text', 
          'name'=>'name', 
          'class'=>'form-control', 
          'required' => 'required',
          'value' => $this->session->userdata('name')
          );
    ?>

在你的控制器中,

$this->session->set_userdata('name',$this->input->post('name'));
if ($data['upload_data']['file_size'] == '0') 
{
    $this->session->set_flashdata('req_msg', 'Cannot Upload Empty File');
    redirect('user/requirement');
} 
else 
{
    if ($this->um->create_requirement_nofile($instanthire_main_id)) 
    {
        $this->session->unset_userdata('name');
        $this->session->set_flashdata('req_msg', 'Requirment raised successfully');
        redirect('user/requirement');
    }
}