Get all selected values from bootstrap multiselect using PHP? - php

Having an off-day today and can't seem to figure this one out for some reason (feeling like a blonde moment).
I am trying to use the bootstrap (3.3.7) multiselect field
EX:
<select multiple class="form-control" id="select_example">
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
<option value="four">4</option>
<option value="five">5</option>
</select>
When I submit (submits via ajax), the MySQL db receives all the data, but for multiselect fields, it is only the value of the last selected option in the options list.
How would I go about getting all of the selected option values and putting them into an array or comma separated string?
Selected: 2,4,5
Current value to db: five
Expected: two,four,five (or something of the like)
Is there a PHP solution for this, or jquery (or combo of the two?)

if($_POST['aircraft']){
$_POST['aircraft'] = implode(', ', $_POST['aircraft']);
}
with a select multiselect with the name aircraft the above code converts the returned array of the multiselect, and converts it into a comma separated string.

<select multiple class="form-control" id="select_example" name="options[]">
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
<option value="four">4</option>
<option value="five">5</option>
$options = implode(',', $_POST['options']);

Related

HTML select Tag Title using Codeigniter 3

I'm using Codeigniter 3.
The countries select box is an optional field on a form:
<select name="countries">
<option>Choose a Country</option>
<option value="en">England</option>
<option value="fr">France</option>
<option value="de">Germany</option>
</select>
I have the following line for the countries field validation:
$this->form_validation->set_rules('year', 'Year', 'trim|integer');
When I submit the form with the 1st option (Choose a Country), even without a value defined the validator sees something like this:
echo '<pre>';
print_r($this->input->post('year'));
echo '</pre>';
>> Choose a Country
.. when I am expecting something like false or null.
How can I make CI ignore the value when there isn't one selected?
Thanks in advance!
First of all, you paste the wrong line, I think it should be countries instead of year.
You can make it by these ways:
Give the default selection a value, that can be caught. And handle when the value equals to none
<select name="countries">
<option value="none">Choose a Country</option>
<option value="en">England</option>
<option value="fr">France</option>
<option value="de">Germany</option>
</select>
Change your validation rule that only allows options in the list. If the value returns isn't in the list, it would make error.
$this->form_validation->set_rules('countries', 'Countries', 'required|trim|in_list[en|fr|de]');

Showing the previously selected value in a multi-select option

Thanks for reading.
I am using the following multiselect option for a users 'interests'. They can pick one or many. The interests are then stored in the database as a string, which is created from the array that the code below supplies. The thing is, when the user returns to modify his/her interests I want the box to display whats already in the database. The value for this is $user->interests (which is a string). I can't see how I can perform this. You can see I added $user->interest below to the value of the . Obviously this doesn't work. Any ideas...?
<select name="interests[]" value="<?php echo $user->interests;?>" style="width:390px;" data-placeholder="Select your specialist interests" class="chosen-select" multiple tabindex="6">
<option value=""></option>
<option value="Acute critical care">Acute critical care</option>
<option value="Allergic diseases">Allergic diseases</option>
<option value="Asthma">Asthma</option>
<option value="Bronchiectasis">Bronchiectasis</option>
<option value="Cardiology">Cardiology</option>
</select>
The select element doesn't have a value attribute.
You need to add a selected attribute to each of the <option>s that should be selected by default.
You should get the list of values in an array and then check to see if each option is in that array as you output them.
Try this variant.
<select name="interests[]" style="width:390px;" data-placeholder="Select your specialist interests" class="chosen-select" multiple tabindex="6">
<option value=""></option>
<option value="Acute critical care"
<?php if(strpos( $user->interests,"Acute critical care")){echo 'selected="selected"'} ?> >
Acute critical care
</option>
//Similarly for other options.
//....
</select>

Removing multiple select option from drop down [duplicate]

This question already has answers here:
Removing an item from a select box
(16 answers)
Closed 9 years ago.
Does anyone knows how to remove multiple options from form drop down? I want to read data from database, and depending on its value remove some options.
<select name="year">
<option value="1">First year/option>
<option value="2">Second year</option>
<option value="3">Third year</option>
</select>
After I read data from the database, I would like to remove some values based on the data's value. So if the data value is 3, I want to remove values 1 and 2 from thr drop down list..
You can use filter() to filter out elements on custom conditions.
Live Demo
$('[name=year] option').filter(function(){
return this.value != '3'
}).remove();
or
Live Demo
$('select :not(option[value=3])').remove();
Edit based on comments, to filter elements have values less then given value.
Use < operator instead of == then and treat the value as number instead of string,
$('[name=year] option').filter(function(){
return parseInt(this.value) < 3
}).remove();
All you have to do is have 2 sections of the same dropdown box. Inside the sections you need to dynamically generate the options omitting the ones which you do not need.
if($data_from_db){
<select name="year">
<option value="1">First year/option>
<option value="2">Second year</option>
</select>
}
else{
<select name="year">
<option value="1">First year/option>
<option value="2">Second year</option>
<option value="3">Third year</option>
</select>
}
You can use something like this
$("select option[value!='3']").remove();

how to get values from multiple select/listboxes in php?

i want to know that how to get multiple selectbox values in php
means that i have more than 1 select box on a page and it can be increased when user click on addmeans that a page can contain numerous selectboxes now plz tell me how to get the values from that boxes and also how do i get know that how many select boxes were used by user
i think an array should be used but i dont know how??
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value2</option>
</select>
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value2</option>
</select>
<select name="select" id="select">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value3</option>
</select>
like in above example
how do i get know that how many selectboxes were used and how to get value of each box
code for adding the new dropdown
function addRow()
{
var newRow = document.all("tblGrid").insertRow();
var oCell = newRow.insertCell();
oCell.innerHTML = "<select name='select' id='select'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select>";
First of all you need to use unique id and name for each select box. Otherwise it is difficult to manage them after post.
So you can do it something like this:
<select name="myselect[]" id="select1">
<select name="myselect[]" id="select2">
<select name="myselect[]" id="select3">
After form post you can get all select box values:
print_r( $_POST['myselect'] );
For number of select boxes on page you can try:
echo count( $_POST['myselect'] );
Change the name attribute value in each tag to something unique like 'select1','select2','select3' . To refer these values in PHP use $_POST['select1'] and so on. OR use $_GET['select1'] incase of get method is used in the form.

optgroup get in php label associated

I have following code in a html form
<select name="category" class="input" onchange="ShowTB(this,'suggest');">
<option value="0" selected="selected">
[choose yours]
</option>
<optgroup label="Item">
<option value="SubItem1"SubItem1</option>
<option value="SubItem2">SubItem2</option>
</optgroup>
<option value="Item2">Item2</option>
<optgroup label="Item3">
<option value="SubItem4"SubItem4</option>
<option value="SubItem5">SubItem5</option>
</optgroup>
<option value="Item4">Item4</option>
<option value="Item5">Item5</option>
<option value="Item6">Item6</option>
<option value="Item7">Item7</option>
</select>
in php i get the value of field selected with:
$category = $_POST['category'];
in this mode if i select in the form ie: SubItem1 , in php i get value SubItem1 but i want also get associated label ie: Item or if i select SubItem5 i get SubItem5 but i want also get associated label ie: Item3
How to ?
Indeed, you only get the value. If you need more, just encode whatever you want into the value, for example:
<option value="Item3.SubItem5">SubItem5</option>
Alternatively, you could use javascript to catch onChange events on the select field and update a hidden input field with the desired label.
you could make the values arrays e.g.
<option value="Item3[SubItem5]">SubItem5</option>
so then your $_POST['category'] should return an array

Categories