Thursday, September 19, 2013

Codeigniter form validation and file upload

I had an issue in implementing form validation and file uploading for the paper submission, because once the file is uploaded, the page reloads and all data is lost. To overcome this I implemented form validation along with file uploading.
Whole page should consist a single form and form validation can be used to validate the data. Then the submit button of the form can be used as the file upload button. This prevents page from reloading.

Study more on this using the following links.
Form Validation
File Uploading

The following code contains a sample of implementing the above theory.

//form in view

<?php echo $formerror;?>
       <?php echo form_open_multipart(base_url('index.php/ongoing_conference/do_upload'));?>
             <input type="file" name="userfile" size="20" />
             <input type="text" id="TitleOfFile" name="TitleOfFile" value="" size="70">
             <input type="submit" value="upload" />
             <?php echo  $uploaderror;?>
       </form>

//controller method

controller


    function do_upload()
    {
        //echo 'true';

            $this->load->helper(array('form', 'url'));
            $this->load->library('form_validation');
            $this->form_validation->set_rules('TitleOfFile', 'File title', 'trim|required|min_length[5]|xss_clean');       
                   
            if ($this->form_validation->run() == FALSE)
            {
                //echo 'form validation false';
                $formerror="form validation false";

                $navtab='includes/navigation';
                $footer='footer';
                $home='Ongoing_conference';
                $login_view='includes/login_view';
                $registration_view='includes/registration_view';
                $paras = array('site_navigations' => $navtab, 'footer' => $footer, 'content' => $home, 'login_view'=>$login_view, 'registration_view'=>$registration_view, 'formerror' => $formerror, 'uploaderror' => $uploaderror);
                $this->load->view('includes/main',$paras);
            }
            else if($this->form_validation->run() == TRUE)
            {
                //echo 'form validation true';

                $config['upload_path'] = './assets/upload/materials/';
                $config['allowed_types'] = 'docx|pdf';
                $config['max_size'] = '2000';
                $config['file_name']  = "yas";
                $config['overwrite'] = TRUE;
                $this->load->library('upload', $config);

                if ( ! $this->upload->do_upload())
                {
                    $uploaderror = $this->upload->display_errors();

                    $navtab='includes/navigation';
                    $footer='footer';
                    $home='Ongoing_conference';
                    $login_view='includes/login_view';
                    $registration_view='includes/registration_view';
                    $paras = array('site_navigations' => $navtab, 'footer' => $footer, 'content' => $home, 'login_view'=>$login_view, 'registration_view'=>$registration_view, 'formerror' => $formerror, 'uploaderror' => $uploaderror);
                    $this->load->view('includes/main',$paras);
                    //echo $uploaderror;
                }
                else
                {
                   
                    $uploaderror='file is uploaded now with correct form validation !';
                    $navtab='includes/navigation';
                    $footer='footer';
                    $home='Ongoing_conference';
                    $login_view='includes/login_view';
                    $registration_view='includes/registration_view';
                    $paras = array('site_navigations' => $navtab, 'footer' => $footer, 'content' => $home, 'login_view'=>$login_view, 'registration_view'=>$registration_view, 'formerror' => $formerror, 'uploaderror' => $uploaderror);
                    $this->load->view('includes/main',$paras);
                }

            }
    }


No comments:

Post a Comment