简体   繁体   中英

Previous dropdown value not getting set with set_value in Codeigniter

The following is my HTML code

<div class="col-md-6">
<label for="martial_status">Martial Status</label>
<select name="martial_status" id="martial_status" class="form-control" value="<?php echo set_value('martial_status'); ?>">
   <option value="">Select Martial Status</option>
   <?php foreach ($martial_status as $status) { ?>
   <option value="<?php echo $status['id'] ?>"><?php echo $status['status_name'] ?></option>
   <?php } ?>
</select>

Even though i have used the form_validation in controller side, the value is not getting set in drop down once the form fails submitting. How to set the previous value selected in drop down on form failure. Where am I going wrong.

You cannot set the value straightly to select tag. You need to set the selected attribute to option tag which you want it to be get selected. Just like it below....

<div class="col-md-6">
<label for="martial_status">Martial Status</label>
<select name="martial_status" id="martial_status" class="form-control" value="<?php echo set_value('martial_status'); ?>">
   <option value="">Select Martial Status</option>
   <?php foreach ($martial_status as $status) { ?>
   <option value="<?php echo $status['id'] ?>" <?php if ($status['id'] == set_value('martial_status')) echo "selected = 'selected'"?>><?php echo $status['status_name'] ?></option>
   <?php } ?>
</select>

On generating the options tag check which option got selected and echo selected = 'selected' in the attribute of that option tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM