CodeIgniter - Repopulating an invalid form w/ redirect CodeIgniter - Repopulating an invalid form w/ redirect codeigniter codeigniter

CodeIgniter - Repopulating an invalid form w/ redirect


Set the the post data as flashdata (part of the session class):

$this->session->set_flashdata('post', $this->input->post());

Then get it:

$this->session->flashdata('post');

You can make the data persist longer as well:

$this->session->keep_flashdata('post');

For More : http://codeigniter.com/user_guide/libraries/sessions.html


One way is to store the POST data in session before redirect e.g. if your form is 'register', then you can do:

session_start();$_SESSION['register_form_data'] = array();foreach($_POST as $key=>$value){    $_SESSION['register_form_data'][$key] = $value;}

Now the form data will be available on form page. You can also create a set_value_from_session() helper function to repopulate the fields.


you could extend CI_Form_validation and add (or add this to CI_Form_validation construct)

$CI =& get_instance(); $CI->load->library('session'); if (count($_POST) > 0 OR count($_FILES) > 0) { $CI->session->set_flashdata('prg', array( 'date' => time()+ini_get('max_execution_time'), 'post' => $_POST, 'files' => $_FILES )); redirect(current_url(), 'location', 302); } else{ $prg = $CI->session->flashdata('prg'); if($prg==TRUE){ if($prg['date']>time()){ $_POST = $prg['post']; $_FILES = $prg['files']; } } }

to the construct.This will fix your issueBut if you have any redirect() functions after form submissions in system, those redirects will be overridden from above redirect. so please pay attention to that.(work-around - you could call this in every function where redirect() is not present )