Welcome Guest, Not a member yet? Register   Sign In
Newbie Questions

[eluser]Ben Lilley[/eluser]
Hi people, this is my first day using CI, and also the start of my first project! Throwing myself in the deep end I know but I feel it's the way I learn the fastest. If possible, I'd like to use this thread to post questions as they pop up, rather than starting a whole bunch of threads, and I'd appreciate any help I can get.

My first issue is inserting data into the database using the form. I've read the user guide and also a couple of tutorials but everyone seems to do it a bit differently. Currently I'm thinking this:

1) Create function in controller called 'add', which loads a view that holds the form.
2) Use fills form in and hits submit which calls another function called 'add_new'
3) 'add_new' calls a model which uses something along the the lines of the code below to insert the data from the form into the db.

First of all is this the best way of doing things? Second of all how do I get the form values from add to add_new and then get add_new to use the model to insert the data into the database.


[eluser]Colin Williams[/eluser]
Take a spin through http://ellislab.com/forums/viewthread/92009/

It shows the basic way to handle this stuff.

[eluser]Ben Lilley[/eluser]
Thanks for the link, I watched all the videos but it did confuse me a little bit, but I saw you're looking at doing a written version which would be wicked so can walk through step by step. I'll definitely give it another go now and see how I get on.


[eluser]Colin Williams[/eluser]
The thing to see is that the controller function that displays the form and the function that processes the form are one in the same. The difference happens within the condition of validation succeeding or failing. And there are actually three conditions

1.) Validation fails, but there are no errors. This means the page has simply been requested. In this case, we should show a blank form.

2.) Validation fails, and there are errors. This means the user submitted the form, but something was incorrect. In this case, we should reload the form, display the errors, and repopulate the fields with the users submitted values.

3.) Validation passes. This means the user successfully submit the form. In this case, we should save the data using the model, then either generate a page detailing the transaction, or redirect them to a relevant page (like one that shows the object they just created.) I prefer redirecting, and I use session flashdata to store messages.

Theme © iAndrew 2016 - Forum software by © MyBB