In my view I have a form select multiple box where I add items using a select dropdown and a button like this.
This is how the code of the select multiple box looks after adding 3 items to it.
<label for="nombresFamilia" class="col-md-4 control-label"></label>
<div class="col-md-6">
<select multiple id="nombresFamilia" name="nombresFamilia[]" class="form-control">
<option value="CABLES">CABLES</option>
<option value="PCR">PCR</option>
<option value="POSTES">POSTES</option>
</select>
</div>
After submitting the form information, I want to save the items inside the select multiple into an array. This is the code I have in my controller.
function add()
{
.
.
.
$this->form_validation->set_rules('nombresFamilia[]','NombresFamilia');
if($this->form_validation->run())
{
.
.
.
$params_familia = $this->input->post('nombresFamilia[]');
}
}
However, when I try to var_dump the array
<?php
echo "<pre>";
var_dump($params_familia);
echo "</pre>";
?>
I get the message: "Undefined variable: params_familia".
What am I doing wrong?
you could pre-select the options of the select, do something like this
VIEW
<select name="nombresFamilia[]" class="" multiple="multiple">
<option value="CABLES" selected="selected">CABLES</option>
<option value="PCR" selected="selected">PCR</option>
<option value="POSTES" selected="selected">POSTES</option>
</select>
CONTROLLER
$nombresFamilia = $this->input->post('nombresFamilia');
// remove [] in nombresFamilia
echo "<pre>";
var_dump($params_familia);
echo "</pre>";
PRODUCES
Array
(
[0] => CABLES
[1] => PCR
[2] => POSTES
)
Checkboxes are named as arrays, dropdowns are not named as arrays . In Drop down or < select > only one value is returned which is selected. if you want to get value of the selected item. Name your select element as
<select multiple id="nombresFamilia" name="nombresFamilia" class="form-control">
<option value="CABLES">CABLES</option>
<option value="PCR">PCR</option>
<option value="POSTES">POSTES</option>
</select>
and then get the value as
$params_familia = $this->input->post('nombresFamilia');
if you want multiple selections then create checkboxes instead of dropdown as
<input type="checkbox" name="nombresFamilia[]" value="CABLES">Cables
<input type="checkbox" name="nombresFamilia[]" value="PCR">PCR
<input type="checkbox" name="nombresFamilia[]" value="POSTES">POSTES
and then get the values in your controller as
$params_familia = $this->input->post('nombresFamilia[]');
Edit as Per comment
If you want to send all the values to the post , create array of checkboxes like
<input type="checkbox" name="nombresFamilia[]" value="CABLES" selected>Cables
<input type="checkbox" name="nombresFamilia[]" value="PCR" selected>PCR
<input type="checkbox" name="nombresFamilia[]" value="POSTES" selected>POSTES
and then post. In your Controller get these values by
$params_familia = $this->input->post('nombresFamilia[]');
You can use the form helper's multiselect function in your view like this in:
$options = array(
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
);
echo form_multiselect('shirts[]', $options);
And then in controller, use the array like this:
print_r($this->input->post('shirts[]'));
Hope this helps.