0
votes

Codeigniter question:

I've just setup my first form with $this->form_validation->set_rules(etc etc), after the validation runs corrects should I post the values using $email = $this->input->post('email', TRUE); or is did the form validation already post the values? If so - how do I return those values?

Thanks, walker

3

3 Answers

2
votes

After your validation is performed, the values still exist in the $_POST array, so depending on your app and your needs, you may or may not need to assign them to variables.

To extend - If you are going to do further processing, then it would be advisable to do $email = $this->input->post('email');, but you could equally just pass the entire $_POST array to a model, for instance, without having to do any extra typing -

$this->my_model->do_something($this->input->post())

Depending on your requirements of course.

1
votes

input->post means whatever is in the $_POST superglobal array in PHP. This is set before Codeigniter does anything with your script - and it also means Codeigniter populates input->post from $_POST. So when you "post" values, you're simply submitting a form with the method set to POST. Make sense?

From there the CI_Controller class handles the rest, and you should be able to access input->post at any time before, during or after validation.

1
votes

Yes -- you are right.

Yout want to retrieve post values with the following syntax:

An easy way to handle update data from a form would be:

// the if syntax is incorrect, i forget the exact wording
if ($this->form->valid()){
   $updateData = array(
      'email' => $this->input->post('email'),
      'username' => $this->input->post('username'),
      'password' => $this->input->post('password')
   );
   // check uniqueness of email
   $this->db->where('email', $updateData['email']);
   $uniqueQuery = $this->db->get('accounts');
   if ($uniqueQuery->num_rows() == 0){
       $this->db->insert('accounts', $updateData);
   }
}