5

I am using HMVC Codeigniter pattern for my project. And In my project I need to set the value on form where input type of field is file.So, I feel greatful for them who help me to solve out this problem.

My controller is test.php

<?php
class Test extends Controller{
function __construct(){
        parent::__construct();  

    }
function index(){

redirect('test/form');


}
  function form(){
        $this->load->library('form_validation');
        $this->load->helper('url');
        $this->form_validation->set_rules('fname','First Name','required|trim');
        $this->form_validation->set_rules('mname','Middle Name','required|trim');
        $this->form_validation->set_rules('lname','Last Name','required|trim');


        if (empty($_FILES['userfile']['name']))
        {
            $this->form_validation->set_rules('userfile', 'Attach Your File', 'required');
        }
        if($this->form_validation->run($this)){

                    $_SESSION['msg']="Your form has been submitted succesfully!!";
                    redirect('test/form');
                }
                else
                {

                    $_SESSION['err']="Opp! There was some wrong to fill up a form so try again!!!";
                    redirect('test/form');


                }

            }
        $data['pgtitle']='Test';       
        $this->load->view('test',$data);
        } 
}

My view file is test.php

<form action="<?php echo site_url()?>test/form" method="post" enctype="multipart/form-data" onsubmit="return confirm('Do you really want to submit the form?');">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">


    <tr>
      <td width="">Name</td>
      <td width="158"><label for="fname"></label>
      <input type="text" name="fname" id="fname" placeholder="First Name" class='input-row' value="<?php echo set_value('fname')?>"/><span class="required"><?php echo form_error('fname');?></span></td>
      <td width="158"><label for="mname"></label>
      <input type="text" name="mname" id="mname" placeholder="Middle Name" class='input-row' value="<?php echo set_value('mname')?>"/></td>
      <td width="158"><label for="lname"></label>
      <input type="text" name="lname" id="lname" placeholder="Last Name" class='input-row' value="<?php echo set_value('lname')?>"/><span class="required"><?php echo form_error('lname');?></span></td>
    </tr>


    <tr>
      <td>Attach Your File</td>
      <td colspan="2"><label for="userfile"></label>
      <input type="file" name="userfile" id="usefile" value="<?php echo set_value('userfile')?>"/><span class="error"><?php echo form_error('userfile')?></span></td>
    </tr>
    <tr>

      <td><input type="submit" name="submit" id="submit" value="Submit" class="Button" /></td>
    </tr>
  </table>
</form >
1
  • you can not, due to security purposes. imagine a browser crawling your filesystem ! ref1 ref2. you may use other workarounds to repopulate item name in a text field Commented Mar 16, 2015 at 10:45

2 Answers 2

1

Set_value will not work because your are redirecting redirect('test/form'); when the validation fails. you have to load form view without redirection than it will work.

if ($this->form_validation->run() == FALSE) { $this->load->view('myform'); }

Sign up to request clarification or add additional context in comments.

Comments

0

I am finding it to be impossible to populate the

<input type='file />

https://html.spec.whatwg.org/multipage/input.html#file-upload-state-(type=file)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.