• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple upload problem

#1
[eluser]markanderson993[/eluser]
I am having considerable trouble getting my file field to be picked up by my controller. It is like the name field is not sending its value over to the controller. Every time I submit the form It gives me the same, "please fill in your file selection field". Ahh! Why does this happen?

If anyone has an explanation for this I would be very thankful.

Here is the controller:

Code:
if (isset($_POST['create_image_group']))
        {
            $image_group_name = $this->input->post('image_group_name');
            
            if ($image_group_name != '')
            {
                $this->upload_model->create_image_group($id,'custom',$image_group_name);
                $data['success'] = 'The image album entitled: <b>'.$image_group_name.'</b> has been successfully created';
            } else {
                $data['error'] = 'Please enter a title for your image group';
            }
        }

And the view

Code:
&lt;?php echo form_open_multipart('images/managealbums/'.$this->uri->segment(3));?&gt;

<div style="margin-top:5px;"><b>Select an album:</b>
<select style="font-size:11px;" name="image_group_id" >
&lt;?php if (isset($image_groups)): ?&gt;
    &lt;?php foreach($image_groups->result() as $row): ?&gt;
    <option value="&lt;?php echo $row->image_group_id?&gt;">&lt;?php echo $row->image_group_name?&gt;</option>
    &lt;?php endforeach; ?&gt;
&lt;?php endif; ?&gt;
</select></div>

<div style="margin-top:5px;"><b>Select an image</b></div>

&lt;input type="file" name="userfile" size="20" /&gt;

<div style="margin-top:5px;"><b>Title <span style="color:#666;font-size:9px;position:relative;top:-1px;">(optional)</span></b><br />&lt;input type="text" name="image_title"&gt;&lt;/div>

<div style="margin-top:5px;">
<b>Description <span style="color:#666;font-size:9px;position:relative;top:-1px;">(optional)</span></b>&lt;textarea name="image_desc" style="padding:2px;font-size:11px;font-family:Arial, Helvetica, sans-serif;width:100%;height:100px;"&gt;&lt;/textarea>
</div>

<div style="margin-top:5px;">&lt;input type="submit" name="upload_album_picture" value="upload" /&gt;&lt;/div>

&lt;/form&gt;

#2
[eluser]swanky[/eluser]
You're right, your controller is not getting the value of image_group_name. It's getting the value of image_group_id instead. (You also might want to think about adding some friendly validation to your form.)

If you want to get the value of your image_group_name you'll want to replace this:

Code:
<select style="font-size:11px;" name="image_group_id" >
&lt;?php if (isset($image_groups)): ?&gt;
    &lt;?php foreach($image_groups->result() as $row): ?&gt;
    <option value="&lt;?php echo $row->image_group_id?&gt;">&lt;?php echo $row->image_group_name?&gt;</option>
    &lt;?php endforeach; ?&gt;
&lt;?php endif; ?&gt;
</select>

With this:

Code:
&lt;?php
if (isset($image_groups))
{
    $groups = array();
    foreach($image_groups->result() as $row)
    {
        $groups[$row->image_group_name] = $row->image_group_name;
    }
    echo form_label('Image Group', 'image_group_name'); // not needed, but good for accessible forms
    echo form_dropdown('image_group_name',$groups);
}

#3
[eluser]markanderson993[/eluser]
Thanks for your input Smile

I'll make some changes. And do you know why the file field is not being picked up by the controller?

Thanks a bunch,
Mark

#4
[eluser]swanky[/eluser]
Well... I don't see your file input 'userfile' referenced at all in the controller code you posted, and I don't see a 'create_image_group' input in your view.

Did you post the right view/controller pair? It looks like the controller is trying to create a new image group and the view is trying to upload an image to an already existing image group.

The user guide has some really handy information on how to build and validate forms in CodeIgniter. :-)

#5
[eluser]markanderson993[/eluser]
Oops! Clumsy me, sorry :/ I posted the wrong portion of the controller! Here is what I really meant to post. Wow, I feel horrible Sad

Code:
# Add Image to Image Group
        
        if (isset($_POST['upload_album_picture']))
        {
            if ($this->input->post('userfile') == '') echo 'No userfile<br />';
        
            $rules['image_group_id'] = "required";
            $rules['userfile'] = "required";
            
            $this->validation->set_rules($rules);
            
            $names['image_group_id'] = "image group";
            $names['userfile'] = "file selection";
            
            $this->validation->set_fields($names);
            
            if ($this->validation->run() == FALSE)
            {
                $data['userfile'] = $this->input->post('userfile');
            } else { // Everything is filled in
                echo 'Everything worked, yyay';
            }
        }
        
        # Handle the page request
        
        if ($this->db_session->userdata('id') != $id) return redirect(''); // If the ID does not match the current user's then redirect
        
        if ($this->db_session->userdata('id') == $id) $data['editing'] = TRUE; // If ID matches the current user's
        $data['profile_image'] = $this->images_model->get_profile_image($id);
        $data['profile_image_tn'] = $this->images_model->get_profile_image_tn($id);
        $data['image_groups'] = $this->images_model->get_image_groups($id);
        $data['username'] = $this->user_info_model->get_user_name($id);

        $this->template->load('images/managealbums_view',$data);
        
    }

And just for kicks and giggles, the view:

Code:
&lt;?php echo form_open_multipart('images/managealbums/'.$this->uri->segment(3));?&gt;

<div style="margin-top:5px;"><b>Select an album:</b>
<select style="font-size:11px;" name="image_group_id" >
&lt;?php if (isset($image_groups)): ?&gt;
    &lt;?php foreach($image_groups->result() as $row): ?&gt;
    <option value="&lt;?php echo $row->image_group_id?&gt;">&lt;?php echo $row->image_group_name?&gt;</option>
    &lt;?php endforeach; ?&gt;
&lt;?php endif; ?&gt;
</select></div>

<div style="margin-top:5px;"><b>Select an image</b></div>

&lt;input type="file" name="userfile" size="20" /&gt;

<div style="margin-top:5px;"><b>Title <span style="color:#666;font-size:9px;position:relative;top:-1px;">(optional)</span></b><br />&lt;input type="text" name="image_title"&gt;&lt;/div>

<div style="margin-top:5px;">
<b>Description <span style="color:#666;font-size:9px;position:relative;top:-1px;">(optional)</span></b>&lt;textarea name="image_desc" style="padding:2px;font-size:11px;font-family:Arial, Helvetica, sans-serif;width:100%;height:100px;"&gt;&lt;/textarea>
</div>

<div style="margin-top:5px;">&lt;input type="submit" name="upload_album_picture" value="upload" /&gt;&lt;/div>

&lt;/form&gt;

Hope this helps? Haha


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.