CodeIgniter Forums
Split form in many pages... better way? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Split form in many pages... better way? (/thread-52913.html)

Pages: 1 2


Split form in many pages... better way? - El Forum - 07-02-2012

[eluser]henry178[/eluser]
Which is the better way for split form in many pages?

view1 -> view2 -> view3 -> confirm (save date in db).

Thanks for help.


Split form in many pages... better way? - El Forum - 07-02-2012

[eluser]JamieBarton[/eluser]
Alernatively you could use jQuery to show the next part of the form and hide previous part. Keeping it all on one page is what I'd suggest.

You could if you wanted to use your way and store the items in a session, or a temporary row in the database. You'd then have to handle any expired entries etc..

I'd go with jQuery.


Split form in many pages... better way? - El Forum - 07-02-2012

[eluser]Matalina[/eluser]
I'm saving to the database on a multipage form but then again that's who my employeer wants it.


Split form in many pages... better way? - El Forum - 07-02-2012

[eluser]PhilTem[/eluser]
There are multiple ways to create a multi-page form:

1) Create a separate view for every form part and insert the previous post data in a hidden field (or hidden fields)
2) Create a separate view for every form part and store the submitted post data in the session (or a cookie)
3) Create a separate view for every form part and store every form's submitted post data into a database
4) Create one view for all form parts and just make them visually separated views (e.g. by using jQuery)

Now you should decide, which way you would like to implement.

PS: Maybe there are even more ways, but I think these are the three most essential and essentially different ways


Split form in many pages... better way? - El Forum - 07-02-2012

[eluser]henry178[/eluser]
Thanks for all Smile


Split form in many pages... better way? - El Forum - 07-02-2012

[eluser]CroNiX[/eluser]
After validation for each step, I'd just serialize the array of form fields ($_POST) from that step and stick them in session (using db sessions). After the last step, retrieve and unserialize all arrays from previous step, merge them, and then insert them all in one go.

Code:
if ($this->form_validation->run())
{
  $this->session->set_userdata('step1', serialize($this->input->post());
  redirect('form/step2');
}
...

$step1 = unserialize($this->session->userdata('step1'));
$step2 = unserialize($this->session->userdata('step2'));
$steps = array_merge($step1, $step2);//make sure there are no duplicate keys before doing this (form fields with same name), and this assumes your form fields are named the same as your db fields...

$this->db->insert('table', $steps);



Split form in many pages... better way? - El Forum - 07-02-2012

[eluser]henry178[/eluser]
PhilTem can you showme an example with hidden fields?

How I could pass the value from controller to second view form?






Split form in many pages... better way? - El Forum - 07-02-2012

[eluser]PhilTem[/eluser]
Basically, you would take @CroNiX' approach but not store the data in a database but a hidden field

Controller:
Code:
// after first submit
$previous_data = $this->input->post();

$data = array(
              'previous_data' => serialize($previous_data)
             );

$this->load->view('second_form', $data);

View:
Code:
echo form_hidden('previous_data', $previous_data);



Split form in many pages... better way? - El Forum - 07-02-2012

[eluser]CroNiX[/eluser]
If you are going to store the previous data in hidden fields, wouldn't that mean you are going to constantly be revalidating the same data, and an ever increasing amount for each step that you progress through?


Split form in many pages... better way? - El Forum - 07-02-2012

[eluser]PhilTem[/eluser]
Well, basically, you won't increase the number of post items by every request. If you did good merging on your new hidden field and the old hidden field, you will basically only get one more field per form page.

The idea is to put all data of one form-page into one hidden field (by either serialization or json_encode'ing). So you will only get one more field per form page. Which you don't have to validate on any page but the page it was entered.

Since you probably want to process all the data the user entered into every form at once at the end you will have to read/progress it after submitting the last form anyway.
Of course it will entail that you have all post data within the last form, but I think, if you have write-queries after every submitted post form to your database it's probably slower than just putting it into one input field.

But that's all just my thoughts on something like this since I never needed to create multi-page forms.