Reputation: 134
I am using codeigniter form validation to verify a select option has a value. I was having problems passing the value of my selected option to my controller so I created a hidden input and used jquery to pass the value on_change. Set select works to repopulate the select option but it does not repass the value to my hidden input. I am new and not sure what the best route is to go? Is there a way to re-pass the value from my select option to hidden input upon failed form validation?
Here is my view:
$('#transfer_from_selector').on('change', function () {
var val = this.value;
$('#transfer_from_input').val(this.value);
});
<select class="form-control" id="transfer_from_selector" name="transfer_from_selector">
<option value="" disabled="" selected="selected" class="">Transfer From</option>
<option value="1" <?php echo set_select('transfer_from_selector', 1); ?>class="">Richfield</option>
<option value="2" <?php echo set_select('transfer_from_selector', 2); ?>class="">Eagan</option>
</select>
<input type="hidden" id="transfer_from_input" name="transfer_from_input" value="">
Here is my controller:
$this->form_validation->set_rules('transfer_from_input', 'Transfer From', 'required|differs[transfer_to_input]');
$data = array(
'transfer_from' => $this->input->post('transfer_from_input'),
);
Upvotes: 0
Views: 565
Reputation: 134
I got it, for some reason I assigned an id and not a name, so it wasn't passing the value correctly. No need to make name an array. I removed the jquery code and just passed the select value to my controller.
Upvotes: 0
Reputation: 4033
no need to write hidden field. use aaray with name for select option.try this i hope it will help you.
<select class="form-control" id="transfer_from_selector" name="transfer_from_selector[]">
<option value="" disabled="" selected="selected" class="">Transfer From</option>
<option value="1" <?php echo set_select('transfer_from_selector', 1); ?>class="">Richfield</option>
<option value="2" <?php echo set_select('transfer_from_selector', 2); ?>class="">Eagan</option>
</select>
Upvotes: 1