It is good for user experience to offer feedback; we do this in the preceding sections with validation_errors()
, but it is also useful to keep user data in form elements to save them having to re-type everything, should there be an error. To do this, we need to use CodeIgniter's set_value()
function.
Make sure that you load $this->load->helper('form'),
from within the __constructor()
of the controller; however, you can always autoload the helper from /path.to/codeigniter/application/config/autoload.php
.
We're going to edit the /path/to/codeigniter/application/views/new_record.php
file.
<?php echo form_open('form/submit_form') ; ?> <?php if (validation_errors()) : ?> <h3>Whoops! There was an error:</h3> <p><?php echo validation_errors(); ?></p> <?php endif; ?> <table border="0" > <tr> <td>First Name</td> <td><?php echo form_input(array('name' => 'first_name', 'id' => 'first_name', 'value' => set_value('first_name', ''), 'maxlength' => '100', 'size' => '50', 'style' => 'width:100%')); ?></td> </tr> <tr> <td>Last Name</td> <td><?php echo form_input(array('name' => 'last_name', 'id' => 'last_name', 'value' => set_value('last_name', ''), 'maxlength' => '100', 'size' => '50', 'style' => 'width:100%')); ?></td> </tr> <tr> <td>User Email</td> <td><?php echo form_input(array('name' => 'email', 'id' => 'email', 'value' => set_value('email', ''), 'maxlength' => '100', 'size' => '50', 'style' => 'width:100%')); ?></td> </tr> <tr> <td>Do you want to be contacted in the future?</td> <td><?php echo 'Yes'.form_checkbox('contact', '1', TRUE).'No'.form_checkbox('contact', '0', FALSE); ?></td> </tr> <tr> <td>What is 10 + 5?</td> <td><?php echo form_input(array('name' => 'answer', 'id' => 'answer', 'value' => set_value('answer', ''), 'maxlength' => '100', 'size' => '50', 'style' => 'width:100%')); ?></td> </tr> </table> <?php echo form_submit('submit', 'Submit'), ?> or <?php echo anchor('form', 'cancel'), ?> <?php echo form_close(); ?>
18.188.152.162