I know how to set the inserted value if it is an input control.
Example :
<input type="text" name="fullname" value="<?php echo isset($_SESSION['regData']['fullname']) ? set_value('fullname', $_SESSION['regData']['fullname']) : set_value('fullname'); ?>">
This code will still display your inserted value after the submit button. But how should I apply it with the dropdown
control?
I tried to do like this but it's not working :
<select name="gender" class="form-control">
<?php
if(!empty($genders)){
foreach ($genders as $row) {
echo '<option value="'.$row['id'].'"';
if($row['id'] == $_SESSION['regData']['gender']){
echo set_select('gender', $_SESSION['regData']['gender']);
} else {
echo set_select('gender', $row['id']);
}
echo '>'.$row['gender_title'].'</option>';
}
}
?>
</select>
What is the correct way to apply it? Thank you.
I found a trick to do this. From the controller I have set a variable to the gender if the form_validation->run()
is failed.
if($this->form_validation->run() == TRUE){
// Other statement goes here
} else {
$this->data['gender'] = TRUE;
}
At my view side I do it like this :
<select name="gender" class="form-control">
<?php
if(!empty($genders)){
foreach ($genders as $row) {
echo '<option value="'.$row['id'].'"';
if(isset($gender)){
echo set_select('gender', $row['id']);
} else {
if(isset($_SESSION['regData']['gender']) && $_SESSION['regData']['gender'] == $row['id']){
echo 'selected';
}
}
echo '>'.$row['gender_title'].'</option>';
}
}
?>
</select>
So when the form is submitted and throwing an error, instead of reading the value of the session, it will read the value based on the user select.