Welcome Guest, Not a member yet? Register   Sign In
Handling form data
#1

[eluser]worchyld[/eluser]
I'm just a bit confused as to how to handle form data with Code Ignitor.

Normally I'd,
a) send the data to the same page, or
b) send the data to a processing page, which then redirects to the launching page.

But I'm confused as to how to actually implement it with CI.

Lets say you have a very simple form where you can add a person's name.

Where does the form action go to? Should it be "/path/to/view/file.php" or the controller? Where does the validation take place? How does it send the error messages back to the user?

Should it be like this?

1. Controller file
2. View file
3. Controller (Processing page)
4. Controller file (step 1) with captured errors found in 3.

Or do you do it all on the same page? This of course causes a problem where if you press F5 or refresh the page, you repost the data.

Any clarification on how to handle form data would be great!

Thank you
#2

[eluser]Michael Wales[/eluser]
I usually use the form helper to generate the form's action.
Code:
<?= form_open('controller/method'); ?>

I tend to always use the same method to receive the form data as I did to display the form to the user. Then, using validation, I respond appropriately.
Code:
if ($this->validation->run()) {
  // They have submitted the form and it passed
  // all of my rules. Let's do some MySQL queries
  // then send them to the appropriate method
  redirect('user/control_panel');
} else {
  // Either they haven't submitted the form yet,
  // or they did - and they didn't pass my rules
  $this->load->view('user/login');
}

Hope this helps a bit.
#3

[eluser]worchyld[/eluser]
I've been looking/reading on the user guide (http://ellislab.com/codeigniter/user-gui...ation.html) on how it does its form handling, and I think that's where its confusing me -- the example handles the form processing, and display via the same controller -- sometimes you may want the form processing handling on another file. But I'll work on that.

Anyway, I'm working my way through the user guide tutorial on how to do form handling/validation. But there is something I don't get.

Question 1:
Why do you have to keep defining the "$this->load->library('validation');" in every controller? If you try to auto-include the validation library in the "Auto-load Libraries" section of the config file -- the validation you write never displays the error message?

Question 2:
Can you use the error message carried in the validation library to carry "good" messages? Let's say you want:
1. A form which you enter data
2. A page (or controller) which handles the data
3. If there is an error, send to 1 with error flag/message
4. If there is no error, send data to db, etc and then send to 1 with good flag/message.

Eventually you could use this as a "template/framework" to work in AJAX to handle the form handling (assuming it isn't too complex), or for some basic form validation (ie; username checking).

Anyway, I'm getting there -- albeit very slowly.




Theme © iAndrew 2016 - Forum software by © MyBB