Welcome Guest, Not a member yet? Register   Sign In
Problem with Form Validation on a page reload [SOLVED]
#1

[eluser]Ludovic-r[/eluser]
Hi everyone!

I try to make a little cms to manage my portfolio with CodeIgniter. So for this I've made a little backend which contains 3 parts (each parts is a different page) :


1st part : upload field (you can upload an image and check if the filesize, extension and so on)

2nd part : image uploaded then you have a small form which contains several fields like 'title' 'description' 'alt' and 3 radio buttons to set 3 different colors picked in the image) and obviously the submit button

3rd part : if there are no errors, everything is stored in the Database and the image appears in the front page with its informations (title, description, color etc)


Here is the problem, It works very well but if you miss a field (at part 2) it must show the errors like 'hey you missed the 'title' field' but because the page is reloaded (if the formvalidation return false) I loose my upload informations and I can't go to step 3

Hope you could help me, it will be very very very appreciated! MANY THANKS!
Sorry for my english and for my code below (I'm not a perfect developer :p)

Here's the code :


The upload.php controller :

Code:
<?php

class Upload extends Controller {
    
    function Upload()
    {
        parent::Controller();
    }
    
    function index()
    {    
        $this->load->view('upload/upload_part1');
    }

    function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '10000';
        $config['max_width']  = '2024';
        $config['max_height']  = '1768';
        $config['encrypt_name']  = TRUE;
        $config['remove_spaces']  = TRUE;

        $this->load->library('upload', $config);
        $this->upload->initialize($config);
    
        $config['image_library'] = 'gd2';
        $config['source_image'] = '';
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = FALSE;
        $config['width'] = 204;
        $config['height'] = 204;
        $config['x_axis'] = 50;
        $config['y_axis'] = 50;
        $config['new_image'] = './uploads/thumbs/';

        $this->load->library('image_lib', $config);
        $this->image_lib->initialize($config);
        $this->image_lib->crop();

                        if ( ! $this->upload->do_upload())
                        {
                            $error = array('error' => $this->upload->display_errors());
                            $this->load->view('upload/upload_part1', $error);
                        }    
                        else
                        {
                            $success['congrats'] = 'CONGRATS, UPLOADED FILE';
                            $this->load->vars($success);
        
                            $data = $this->upload->data();
        
                               if($data['is_image'] == 1) {
                                 $config['source_image'] = $data['full_path'];
                                 $this->image_lib->initialize($config);
                                 $this->image_lib->crop();
                               }

                            $data = array('upload_data' => $this->upload->data());
                            $this->load->view('upload/upload_part2', $data);    
                        }
    }    
    
    function color()
    {    
        $data = array('upload_data' => $this->upload->data());
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('alt', 'alt', 'required|trim|xss_clean|encode_php_tags');
        $this->form_validation->set_rules('title', 'title', 'required|trim|xss_clean|encode_php_tags');
        $this->form_validation->set_rules('description', 'description', 'required|trim|xss_clean|encode_php_tags');
        $this->form_validation->set_rules('rd', 'title', 'required|trim|xss_clean|encode_php_tags');
        
                        if ($this->form_validation->run() == FALSE)
                        {    
                            $this->load->view('upload/upload_part2', $data);
                        }
                        else
                        {
                            $data['my_key_name'] = $this->uri->segment(3);
                            $datestring = "%Y-%m-%d %h:%i:%a";
                            $time = time();

                            $date = mdate($datestring, $time);
    
                            $data = array(
                                          'color' => $this->input->post('rd') ,
                                                        'filename' => $this->input->post('fname') ,
                                                        'thumbname' => $this->input->post('tname') ,
                                                        'date' => $date ,
                                                        'description' => $this->input->post('description') ,
                                                        'title' => $this->input->post('title') ,
                                                        'alt' => $this->input->post('alt')
                                        );
    
                            $this->db->insert('mytable', $data);
    
                            $success['congrats'] = 'CONGRATS! STORED IN THE DATABASE';
                            $this->load->vars($success);
    
                            $this->load->view('upload/upload_part3');            
                        }
    }
    
}
#2

[eluser]Ludovic-r[/eluser]
The 1st part form :

Code:
<html>
<head>
<title>PART 1</title>

<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/styles.css" />
</head>
<body>

    <h1>PART 1</h1>

    &lt;?php if (isset($error)) { echo $error; } ?&gt;

    &lt;?php echo form_open_multipart('upload/do_upload');?&gt;


                <label for="id">Upload your file fool</label>
                <br />
                &lt;input type="hidden" name="id" size="20" /&gt;

                <br /><br />
                <label for="userfile">Your File</label>
                <br />
                &lt;input type="file" name="userfile" size="20" /&gt;

                <br /><br />

                &lt;input type="submit" value="upload" /&gt;

    &lt;?php echo form_close(); ?&gt;

&lt;/body&gt;
&lt;/html&gt;
#3

[eluser]Ludovic-r[/eluser]
The 2nd part form (here's the problem)

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;PART 2&lt;/title&gt;

&lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url();?&gt;css/styles.css" /&gt;
&lt;/head&gt;
&lt;body&gt;

<h1>PART 2</h1>

    <ul>
    &lt;?php foreach($upload_data as $item => $value):?&gt;
    <li>&lt;?php echo $item;?&gt;: &lt;?php echo $value;?&gt;</li><br >
    &lt;?php endforeach; ?&gt;
    </ul>

<h1>    &lt;?php if (isset($congrats)) { echo $congrats; } ?&gt; </h1>


<br /><br />
&lt;?php $urli = base_url().'uploads/thumbs/'.$upload_data['raw_name'].'_thumb'.$upload_data['file_ext']; ?&gt;
&lt;?php echo $urli; ?&gt;
<br />
<img src="&lt;?php echo base_url().'uploads/'.$upload_data['file_name']; ?&gt;" alt="temporary_image" />
<br /><br />



            &lt;?php echo form_open_multipart('upload/color');?&gt;
            
                        &lt;?php echo validation_errors('<p class="errors">'); ?&gt;
            
                        &lt;input type="hidden" name="fname" value="&lt;?php echo $upload_data['file_name']; ?&gt;" size="20" /&gt;
                        &lt;input type="hidden" name="tname" value="&lt;?php echo $upload_data['raw_name'].'_thumb'.$upload_data['file_ext']; ?&gt;" size="20" /&gt;
                        <br />
            
                        <label for="category">Your category</label>
                        <br />
                        <select name="category">
                        <option selected>Plan 1
                        <option>Plan 2
                        <option>Plan 3
                        <option>Plan 4
                        <option>Plan 5
                        </select>
                        <hr></hr>

                        <label for="title">Your title</label>
                        <br />
                        &lt;input type="text" name="title" size="20" /&gt;
                        <br />

                        <label for="description">Your description</label>
                        <br />
                        &lt;textarea rows="4" cols="40" name="description"&gt;
                        Enter your description here.
                        &lt;/textarea&gt;
                        <br />

                        <label for="alt">Your alternative text</label>
                        <br />
                        &lt;input type="text" name="alt" size="20" /&gt;
                        <br />
    
                        <br /><br />
                        
                        
                        &lt;?php
                        $ex=new GetColors();
                        $ex->image=$config['upload_path'] = './uploads/'.$upload_data['file_name'];
                        $colors=$ex->Get_Color();
                        $how_many=2;
                        $colors_key=array_keys($colors);
                        $clr=0;

                        ?&gt;
                        
                        &lt;?php for ($i = 0; $i <= $how_many; $i++) { ?&gt;

                                    &lt;input type="radio" name="rd" value="&lt;?php echo $colors_key[$i]; ?&gt;"&gt;
                                    <span style="font-size:21px; padding:30px; width:40px; height:40px; background-color:#&lt;?php echo $colors_key[$i]; ?&gt;; color:#&lt;?php echo $colors_key[$i]; ?&gt;"></span>

                        &lt;?php } ?&gt;&lt;input type="submit" value="upload" /&gt;
                        
                        
            &lt;?php echo form_close(); ?&gt;

    
    <br /><br />


<p><br />Page rendered in {elapsed_time} seconds</p>

&lt;/body&gt;
&lt;/html&gt;
#4

[eluser]boldyellow[/eluser]
Not sure I fully understand, but sounds like you are not saving values in the inputs when the form reloads for an error.

I'm using form helper which makes it easy to set a value:

Code:
&lt;?=form_input('teaser', set_value('teaser'); ?&gt;

So 'teaser' gets posted and if there's an error, the field is repopulated with the posted info

There's more info in the documentation:

Scroll down to Re-populating the form
#5

[eluser]Ludovic-r[/eluser]
[quote author="boldyellow" date="1287877147"]Not sure I fully understand, but sounds like you are not saving values in the inputs when the form reloads for an error.

I'm using form helper which makes it easy to set a value:

Code:
&lt;?=form_input('teaser', set_value('teaser'); ?&gt;

So 'teaser' gets posted and if there's an error, the field is repopulated with the posted info

There's more info in the documentation:

Scroll down to Re-populating the form[/quote]

First, thanks for your answer (it helps me a lot) I have just a (stupid) question :

I've set my values with set_value('...') but what should I do to pass them into my controller (the $this->validation->set_fields($fields); is deprecated), let me explain :

I now have this :

Code:
$data = array(
                                    'fname' => $upload_data['file_name'],
                                    'tname' => $upload_data['raw_name'].'_thumb'.$upload_data['file_ext'],
                                  );

                        echo form_hidden('fname', set_value('fname', $data['fname']));
                        echo form_hidden('tname', set_value('tname', $data['tname']));

And I would like to retrieve those informations when I reload the page if there are some errors :

Code:
if ($this->form_validation->run() == FALSE)
                        {    
                            $this->load->view('upload/upload_part2');
                        }

What should I do ? Create an array and $_POST those informations then pass this array as an attribute ?

Thanks so much for your time Wink
#6

[eluser]Ludovic-r[/eluser]
One more thing, if I add this :

Code:
if ($this->form_validation->run() == FALSE)
                        {    
                            $data = array('upload_data' => $this->upload->data());
                            $this->load->view('upload/upload_part2', $data);
                        }

It works (I mean with no php errors) and reloads the page without the uploads data that I need.

I just want to keep the uploads data when I reload the page if there are some errors. Sad

Any ideas ?
#7

[eluser]boldyellow[/eluser]
[quote author="Ludovic-r" date="1287905456"]


I just want to keep the uploads data when I reload the page if there are some errors. Sad

Any ideas ?[/quote]


If you mean repopulate the upload form field, won't do that:

http://ellislab.com/forums/viewthread/143298/#704468
#8

[eluser]Ludovic-r[/eluser]
Yay I know but how can I store my upload data to keep them when I reload the form page, I've tried session but it doesn't work
#9

[eluser]Ludovic-r[/eluser]
Ok I've solved this after many many tries and coffee :

Just store my $data in a session then put this on the DB then if everything is okay destroy the session.

That's really easy, shame on me!
#10

[eluser]no-cost lab[/eluser]
Hi there!
i have the same problem,
would you please post an example for your solution!
Thanks in Advance ;-)




Theme © iAndrew 2016 - Forum software by © MyBB