Welcome Guest, Not a member yet? Register   Sign In
(beginner) Uploading pictures from a form and validating the form
#1

[eluser]rijobo[/eluser]
Hello,

I've got another question. I wan't to upload pictures to the server and in the database along with some other fields.

I open my form with:

Code:
<?php echo form_open_multipart('producttoevoegen/upload'); ?>

So I only call the upload function. I would also like to validate my form, so I think my upload function should move inside my index function.
Is that the right way, or should I do that different?

Controller:

Code:
<?php

class Producttoevoegen extends MY_Controller {
    function Producttoevoegen()
    {
        parent::MY_Controller();
        if(!$this->auth->is_logged_in())
        {
             $this->error_redirect('U bent niet ingelogd', 'inloggen');
        }
        $this->load->model('Productupload');
     }
    
     function index()
    {
        
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('naam', 'Naam', 'required');
        $this->form_validation->set_rules('omschrijving', 'Omschrijving', 'required');
        $this->form_validation->set_rules('soort', 'Soort', 'required');
        $this->form_validation->set_rules('steen', 'Steen', 'required');
        $this->form_validation->set_rules('prijs', 'Prijs', 'required');
        $this->form_validation->set_rules('afmeting', 'Afmeting', 'required');
        $this->form_validation->set_rules('materiaal', 'Materiaal', 'required');
                
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('product_toevoegen');
        }
        else
        {
            $this->load->view('product_succes');
        }

    }
    
    function upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'png|gif|jpg';
        $config['max_size']  = '10000';
        $upload_veld = 'foto';
        
        $this->load->library('upload', $config);
        
        if($this->upload->do_upload($upload_veld)) {
            $upload_data = $this->upload->data();
        } else {
            $data['error'] = $this->upload->display_errors();
        }
        
        $product = array('naam'=>$this->input->post('naam'), 'omschrijving'=>$this->input->post('omschrijving'), 'foto'=>$upload_data['file_name'], 'soort'=>$this->input->post('soort'), 'steen'=>$this->input->post('steen'), 'prijs'=>$this->input->post('prijs'), 'afmeting'=>$this->input->post('afmeting'), 'materiaal'=>$this->input->post('materiaal'));
        
        $this->Productupload->insertRow('product', $product);
                
    }    
}
#2

[eluser]pistolPete[/eluser]
Does this help you?
http://ellislab.com/forums/viewreply/537022/
#3

[eluser]rijobo[/eluser]
I thought I did, so I put it all in my index function like this. But know I get this error:

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: upload_data

Filename: controllers/producttoevoegen.php

Line Number: 48

Controller:

Code:
<?php

class Producttoevoegen extends MY_Controller {
    function Producttoevoegen()
    {
        parent::MY_Controller();
        if(!$this->auth->is_logged_in())
        {
             $this->error_redirect('U bent niet ingelogd', 'inloggen');
        }
        $this->load->model('Productupload');
     }
    
     function index()
    {
        
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'png|gif|jpg';
        $config['max_size']  = '10000';
        $upload_veld = 'foto';
        
        $this->load->library('upload', $config);
        $this->load->library('form_validation');
        
        if($this->upload->do_upload($upload_veld)) {
            $this->form_validation->set_rules('naam', 'Naam', 'required');
            $this->form_validation->set_rules('omschrijving', 'Omschrijving', 'required');
            $this->form_validation->set_rules('soort', 'Soort', 'required');
            $this->form_validation->set_rules('steen', 'Steen', 'required');
            $this->form_validation->set_rules('prijs', 'Prijs', 'required');
            $this->form_validation->set_rules('afmeting', 'Afmeting', 'required');
            $this->form_validation->set_rules('materiaal', 'Materiaal', 'required');
            $upload_data = $this->upload->data();
            
                
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('product_toevoegen');
        }
        else
        {
            $this->load->view('product_succes');
        }
        } else {
            $data['error'] = $this->upload->display_errors();
        }
        
        $product = array('naam'=>$this->input->post('naam'), 'omschrijving'=>$this->input->post('omschrijving'), 'foto'=>$upload_data['file_name'], 'soort'=>$this->input->post('soort'), 'steen'=>$this->input->post('steen'), 'prijs'=>$this->input->post('prijs'), 'afmeting'=>$this->input->post('afmeting'), 'materiaal'=>$this->input->post('materiaal'));
        
        $this->Productupload->insertRow('product', $product);
        
        

    }
    
    
}
#4

[eluser]rijobo[/eluser]
I've got it like this now, but I the call to the view (product_toevoegen, the form) is on the wrong place.

Code:
function index()
    {
        
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'png|gif|jpg';
        $config['max_size']  = '10000';
        $upload_veld = 'foto';
        
        $this->load->library('upload', $config);
        $this->load->library('form_validation');
        $this->load->view('product_toevoegen');
        
        if($this->upload->do_upload($upload_veld)) {
            $this->form_validation->set_rules('naam', 'Naam', 'required');
            $this->form_validation->set_rules('omschrijving', 'Omschrijving', 'required');
            $this->form_validation->set_rules('soort', 'Soort', 'required');
            $this->form_validation->set_rules('steen', 'Steen', 'required');
            $this->form_validation->set_rules('prijs', 'Prijs', 'required');
            $this->form_validation->set_rules('afmeting', 'Afmeting', 'required');
            $this->form_validation->set_rules('materiaal', 'Materiaal', 'required');
            $upload_data = $this->upload->data();
            $product = array('naam'=>$this->input->post('naam'), 'omschrijving'=>$this->input->post('omschrijving'), 'foto'=>$upload_data['file_name'], 'soort'=>$this->input->post('soort'), 'steen'=>$this->input->post('steen'), 'prijs'=>$this->input->post('prijs'), 'afmeting'=>$this->input->post('afmeting'), 'materiaal'=>$this->input->post('materiaal'));
            
                
            if ($this->form_validation->run() == FALSE)    {
                $this->load->view('product_toevoegen');
            }
            else
            {    
                $this->Productupload->insertRow('product', $product);
                $this->load->view('product_succes');
            }
        
        } else {
            $data['error'] = $this->upload->display_errors();
        }
        
        
        
        
    }
#5

[eluser]rijobo[/eluser]
Can anyone help me with this?
product_toevoegen is my upload page and product_succes is my succes page.
Now, when I've done my upload, the succes page shows up under my upload page, but i want CI to load the succes page as a single new page.




Theme © iAndrew 2016 - Forum software by © MyBB