may i know how do i ensure that the user have selected, Dr, Mr, Ms, Mdm and when they submit the form if the salutation is given as blank it will return an error message for the set_rules().
Code:
echo "<p>Salutation: ";
$salutationOptions = array(
'' => '',
'Dr' => 'Dr',
'Mr' => 'Mr',
'Ms' => 'Ms',
'Mdm' => 'Mdm',
);
echo form_dropdown('salutation', $salutationOptions, '');
echo "</p>";
In the view file, you can do a client side validation using this:
echo "<p>Salutation: ";
$salutationOptions = array(
'' => '',
'Dr' => 'Dr',
'Mr' => 'Mr',
'Ms' => 'Ms',
'Mdm' => 'Mdm',
);
echo form_dropdown('salutation', $salutationOptions, '', 'required="required"');
echo "</p>";
When the user tries to submit without choosing form the dropdown, it will give them an error saying they should choose from the dropdown.
If you want it on the server side, you can do something like this:
$this->form_validation->set_rules('salutation', 'Salutation', 'required')
if($this->form_validation->run()){
/*user has selected. use the data now*/
}else{
/*user has not sleected data, throw error back*/
}
Use required
in HTML , or
do like this
$this->form_validation->set_rules( "Salutation","Salutation","required");
I hope this might help you
https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules
example
in view part:
$options = array('0' => 'SELECT SHIRT', '1' => 'Small Shirt', '1' =>
'Medium Shirt', '2' => 'Large Shirt', '3' => 'Extra Large Shirt', );
echo form_open('welcome/index');
echo form_error('field name to display error');
echo form_dropdown('field name', $options);
echo form_submit('submit', 'submit');
echo form_close();
in controller part:
$this->load->helper('form_validation');
$this->form_validation->set_rules('field name', 'name have to display in the error message', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('your page name'); }else{
$this->load->view('another page');}
Better you would go with foreach() loop. This is the way which I came across.
<?php foreach($classes as $class) { ?>
<option value="<?php echo $class->classesID; ?>" <?php echo set_select('class', $class->classesID); ?>>
<?php echo $class->classes; ?>
</option>
<?php } ?>