Welcome Guest, Not a member yet? Register   Sign In
Unset post data.
#1

[eluser]JasonS[/eluser]
I need to unset post data to stop my form reloading it when I return a success message. I do not want to have to redirect a user back to the controller... or at least.. I shouldn't need to do this. How can I clear the post data?

Code:
public function Add()
{    
    // Set access level
    set_access('admin');
    
    $data = array();
    
    // Add a portfolio item
    // Fields : Title[r], Image, Content, URL, Client[r], Tags[r]
    $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
    
    if (isset($_POST))
    {
        // Run Form Validation
        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('client', 'Client', 'required');
        $this->form_validation->set_rules('tags', 'Tags', 'required');
        
        if ($this->form_validation->run())
        {
            // Validate update and check file uploads
            $config['upload_path'] = './uploads/portfolio/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size']    = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            
            $this->load->library('upload', $config);
            
            // If upload fails
            if ( ! $this->upload->do_upload())
            {
                $data['error'] = $this->upload->display_errors('<p class="error">', '</p>');
            }    
            else // Add to database.
            {
                if ($this->port_model->save($this->upload->data()))
                {
                    $data['success'] = 'This item has been successfully added to the database.';
                    unset($_POST);
                }
                else
                {
                    $data['error'] = 'An error occurred when adding file to the database. Please try again.';
                }
            }
        }        
    }
        
    $this->load->view('portfolio/add_form', $data);
}
#2

[eluser]Unknown[/eluser]
From my limited knowledge, I'm not sure that your form is repopulating with $_POST data. Are you using the form helper in your view or repopulating the fields some other way? I can see that you're clearly unsetting $_POST data if the data was sucessfully saved. Not only that, but you're not even passing $_POST data back to the view - you're passing $data, which appears as though it wouldn't contain anything other that 'success' or 'error' vars. Is it possible that because you're reloading back to the same page that your browser is repopulating those fields? If your view really is reloading the $_POST data, what if you try and set those values to null after saving data? (Ie, $_POST['myData']=''Wink

Have you tried forcing no-cache on the page after data is successfully saved?

After 'unset($_POST)', try adding:

$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
#3

[eluser]brookerrj[/eluser]
I had the same problem and found this on another forum:

"The set_value function fetches its value from the Form_validation object and not from the $_POST array. The Form_validation object stores its own copy of the posted values in a variable called $_field_data."

So after success I used:

Code:
reset($this->form_validation->_field_data);

And it worked for me though considered a bit of a hack.
#4

[eluser]Réjôme[/eluser]
I had the same problem (when the validation is successful, the same page is shown, with a success message).

So, here is my workaround :

First, extend the Form Validation class :

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {
    
    function unset_field_data()
    {    
        unset($this->_field_data);    
        log_message('debug', "Form Validation Field Data Unset");
    }
}

Then call the new function in your controller :

Code:
$this->form_validation->unset_field_data();

Hope that could help.

PS : I'm a bit suprised this is not in the standard class.
What about including it in future versions of CI ?
#5

[eluser]Eric Barnes[/eluser]
[quote author="Réjôme" date="1294164668"]
PS : I'm a bit suprised this is not in the standard class.
What about including it in future versions of CI ?[/quote]

Consider adding this to the feature requests forum: http://codeigniter.uservoice.com/forums/...er-reactor
#6

[eluser]Unknown[/eluser]
Consider adding:
$this->_field_data = array();
after the unset line

This covers a case where set_select issues a warning if $this->_field_data is unset.




Theme © iAndrew 2016 - Forum software by © MyBB