views:

609

answers:

2

I just started using CI today.. I normally use CakePHP but a whole lot of smaller projects needed something less "bulky".

What is the easiest way to implement sticky forms (retain user input after reload) in CI?

I currently have a whole lot of radio buttons for multiple choice quiz and some other text fields and when validation fails the data is lost...

I probably don't need to use session vars as I am not redirecting, I am simply loading the view upon validation failure, so the data should be available in $_POST..

A: 

Use javascript for validation instead of refreshing the page.

Gary Willoughby
Because nobody ever browses with JavaScript turned off.
MrChrister
I'd rather take care of the 95% of people with javascript turn on to make their experience more pleasurable than to gimp the site's responsiveness for the 5% that don't.http://www.w3schools.com/browsers/browsers_stats.asp
Gary Willoughby
+2  A: 

Have you looked at the Validation library? Most of CodeIgniter's built-in form handling is there. Use the Form Helper's set_value() function to reset the values after the form is reloaded:

<input type="text" name="somefield" value="<?php echo set_value('somefield', 'default'); ?>" size="50" />

Note that for set_value() to work, you need to run validation on that field. If you don't need to validate every field in your form, you can set an empty rule, just to make the Validation library aware of that field:

$this->validation->set_rules(array('somefield' => ''));

For the radio buttons, see the Form Helpers set_radio() function.

Adam Bradley
Thanks, i figured this out after going through the docs a couple more times...
bananarepub