CodeIgniter Forums
repopulate from session? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: repopulate from session? (/showthread.php?tid=17908)

Pages: 1 2


repopulate from session? - El Forum - 04-19-2009

[eluser]shreder[/eluser]
Hey everyone!

a quick question:
I'm using the form helper, form validation and the session library.
I'm using the function set_value() to repopulate a form and it works fine when displaying the errors - the fields are populated properly.

I have a few steps wizard with 1 controller and few views (step1.php, step2.php etc...).
On each step, if no errors are found I set few sessions with the field name and the field value that was entered by the user.

On step5 I have a confirmation page that loads all the data from the different sessions and displays them.

Now, my question is this:
Can I use the set_value() function to populate a field when there is no session set and if there's a session set then just load the value from the session?

I want to have a back button that simply goes back 1 step if the user wants to correct something but right now the form is loaded and is empty once the user goes back.

OR if you have any other way of doing this I would be happy to hear it!

Thanks in advance!


repopulate from session? - El Forum - 04-19-2009

[eluser]slowgary[/eluser]
Having 5 pages requires the user to wait for each page to load, and is more taxing on the server. Instead, why not just display the form on 1 page, but use JavaScript to separate it into a tab or accordian view. Your tab buttons don't have to look like tabs, they could easily look like Step 1 -> Step 2 -> Step 3.


repopulate from session? - El Forum - 04-20-2009

[eluser]shreder[/eluser]
Hey.

Thanks for the reply.
Unfortunately, I'm not too familiar with JavaScript yet therefor I usually try to avoid it.
If you can give me an example of what you mean I'll gladly use it.

Thank you!


repopulate from session? - El Forum - 04-20-2009

[eluser]xwero[/eluser]
The javascript solution only works when the pages are not depending on one another and if you want to check the content of the fields you have to use ajax.

A php only solution is to add the values of the forms to the following form but this means all form fields need to have unique names.
Code:
function wizard()
{
   // do the checks here
   $data['posted'] = form_hidden($_POST);
   // change the forms here
}
// views
<form action="" method="post"><?php echo $posted ?>
This works because the hidden inputs are overwritten by the field inputs from the form the user is on but the values from other fields are untouched.


repopulate from session? - El Forum - 04-20-2009

[eluser]shreder[/eluser]
Thanks for the solusion xwero but don't you think using sessions will be simpler instead of passing hidden values between forms?

I would appreciate if anyone could post a quick javascript solution for a multiple step wizard.

Thanks a lot!


repopulate from session? - El Forum - 04-20-2009

[eluser]Dam1an[/eluser]
The one potential problem with using sessions to pass the data between each step, is you may go over the 4k limit (remember the session is encrypted, so larger then you think)


repopulate from session? - El Forum - 04-20-2009

[eluser]shreder[/eluser]
If I'm not mistaken then it's 4K Per session, right?
What if I set a session for EACH field? I doubt it will pass the 4K limit...

Thanks.


repopulate from session? - El Forum - 04-20-2009

[eluser]slowgary[/eluser]
I recommend the jQuery framework... here's the demo for tabs -> http://jqueryui.com/demos/tabs/


repopulate from session? - El Forum - 04-20-2009

[eluser]shreder[/eluser]
Is there a CI library for jQuery maybe?


repopulate from session? - El Forum - 04-20-2009

[eluser]Colin Williams[/eluser]
Sessions can hold all the data you need if you use database sessions. All the cookie stores is the session key.

What's funny is that your question is essentially, "How to I pass these values to a view?" The answer is always the same: Either as a second parameter in $this->load->view() or with $this->load->vars()

Code:
$values['field'] = $this->session->userdata('field');
$values['field2'] = $this->session->userdata('field2');
$this->load->vars($values);