Welcome Guest, Not a member yet? Register   Sign In
Form_validation - how to empty fields data after successful form submission?
#1

[eluser]setgreen[/eluser]
Hi,

I'm experiencing some troubles trying to work with the Form_validation class and I hope you guys could help me.

Here is the problem :

I have a controller which load a view presenting a form.
I use the set_value() function to repopulate the form in the view but when the form has been submitted, I would like the field to be empty again so that the user could use the form again. I know that I could do a simple redirect() to redirect to the same controller but I need to display a feedback message saying everything goes well with the form submission. That feedback message is stored in a $data['feedback'] variable, which is passed to the load view using $this->load->view().

Here is the controller code :

Code:
public function pages_ajout() {
  
  //Validation
  $this->load->library('form_validation');
  
  $rules = array(
                array(
                     'field'   => 'titre',
                     'label'   => 'Titre',
                     'rules'   => 'xss_clean|required'
     )
            );

  $this->form_validation->set_rules($rules);
  
  if ($this->form_validation->run() == FALSE) {
  
   // Do nothing
  
  } else {
  
   $insert = $this->Dbset->setPage($this->input->post('titre'));
  
   if ($insert) {
    $data['feedback']= "Your new page has been submitted !";
   } else {
    $data['feedback']= "Sorry, there was an error...";
   }
  
  }
  
  $this->load->view('back/contenus_pages_ajout_view',$data);
  
}

#2

[eluser]oliur[/eluser]
redirect is the way to go.

Use session flash data to store a message.

Code:
else {
  
   $insert = $this->Dbset->setPage($this->input->post('titre'));
  
   if ($insert) {
    $data['feedback']= "Your new page has been submitted !";
// create feedback message  
$this->session->set_flashdata('feedback','Your message has been succesfully submitted');

   } else {
    $data['feedback']= "Sorry, there was an error...";
   }
  
  }
  
  $this->load->view('back/contenus_pages_ajout_view',$data);


Now in your view file add the following code before the form

Code:
// read flash data if there is any
$feedback_message = $this->session->flashdata('feedback');
if(!empty($feedback_message)){
echo $feedback_message;
}

// form....

Find out more about session
session flashdata
#3

[eluser]setgreen[/eluser]
I never used flashdata before but it seems to fit my needs Smile

Thank you very much for your answer.




Theme © iAndrew 2016 - Forum software by © MyBB