Removing multiple select option from drop down [duplicate] - php

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();

Related

Get all selected values from bootstrap multiselect using 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']);

Switching Dropdown List [duplicate]

This question already has answers here:
Using Ajax, jQuery and PHP in two dropdown lists
(2 answers)
Closed 8 years ago.
I was making a dropdown switch. I mean when I choose an option of the first drop down the second will show specified message preset's in stead of the first.
**Dropdown one**
<select name="bantype">
<option value="user">User ban</option>
<option value="ip">IP ban</option>
<option value="system">System ban</option>
</select></br></br>
**Dropdown two**
<select name="reason">
<option value="Sexual harassment">Sexual harassment</option>
</select>
How can I make that when I select an option from the first dropdown that the option values of the second will switch?
Thank you for help
It's called hierselect, chained or related selects.
I like this jquery plugin: http://www.appelsiini.net/projects/chained
So you have predefined values for each dropdown value from first list
<select id="first">
<option value='ban' data-show="#optionsOne">Ban Ban</option>
<option value='banAll' data-show="#optionsTwo">Ban All</option>
</select>
And you have other (hidden) dropdown for each of your options:
<select id="optionsOne" class='drop'>...</select>
<select id="optionsTwo" class='drop'>...</select>
When #first changes values, show relative dropdown:
$('#first').change(function(){
$('.drop').hide(); // hide all dropdowns
id = $('option:selected', this).data('show'); // take relevant id
$(id).show(); // show that dropdown with above id
});
Call javascript function event onchange,
<select name="bantype" onchange="getoptions(this.value);">
<option value="user">User ban</option>
<option value="ip">IP ban</option>
<option value="system">System ban</option>
</select><br/><br/>
<select name="reason" id="reason"></select>
In your function, use following
getoptions(val) {
if(val == 'user') {
var your_options = '<option value="abc">Abc</option>';
jQuery('#reason').html(your_options);
jQuery('#reason').show(); // if second dropdown is hidden
}
}
If you have a fixed option on page load, then here is one way to achieve what you want.
Give a relation between first select and the second one, like this:
<select name="bantype">
<option value="user">User ban</option>
<option value="ip">IP ban</option>
<option value="system">System ban</option>
</select></br></br>
**Dropdown two**
<select name="reason">
<option class="user" value="a">a</option>
<option class="user" value="d">d</option>
<option class="ip" value="b">b</option>
<option class="system" value="c">c</option>
</select>
Use jQuery to show and hide the option, like this
$("select[name='reason'] option").each(function(){
$(this).hide();
});
$("select[name='bantype']").on("change", function(){
var selected = $(this).val();
$("select[name='reason'] option").each(function(){
if($(this).prop("class") != selected){
$(this).hide();
}else{
$(this).show();
}
});
});
Check out this DEMO..

Collect and echo submission via multiple <select> tags in submission form?

My basic goal is to "capture" three values from three menus (Year, month, day), and print them on the next page pointed to by the submission form. I've been trying all day to find a way to do such - No luck whatsoever, I can only manage to print one of the three values.
An abbreviated version of my select menus:
<form action="processform.php" method="POST">
<select name="birthdate_year[ ]">
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
</select>
<select name="birthdate_month[ ]">
<option value="01">January</option>
<option value="02">Feburary</option>
<option value="03">March</option>
</select>
<select name="birthdate_day[ ]">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
</select>
<input type="submit" value="Submit">
</form>
...And the following shows how I've managed to only get one value to become an array:
foreach( $_POST['birthdate_year'] as $year ) /*Neither &&/|| work, and I cannot "add" to it or add another "foreach" */
{
print $year;
}
I've been scouring the internet for a way to do such - The only working method is "foreach", which can only seemingly convert one value to an array (It won't accept "AND" statements, or for that matter, another "foreach").
All I want is to have each entered value become it's own array - All of whom need to be accessed multiple times to complete the program's function (Compute if whether the user is above/below a cutoff age & storing their birthdate for later examination when they become old enough)
Programs for storing their age and determining their age "group" have already been written - Now I just need each value to become it's own array.
Solution found
For some reason it wouldn't accept multiple "foreach" statements if they weren't descending in some weird way - So now that they're doing so, it works.
What you want is:
2003
2004
2005
<select name="birthdate[month]">
<option value="01">January</option>
<option value="02">Feburary</option>
<option value="03">March</option>
</select>
<select name="birthdate[day]">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
</select>
<input type="submit" value="Submit">
</form>
Notice that the indexes have no quotes. As you do it, you have to check the following variables:
$_POST['birthdate_month'][0]
$_POST['birthdate_day'][0]
$_POST['birthdate_year'][0]
which is not so nice.

transfered data from a select to another and vice versa [duplicate]

This question already has an answer here:
exclude value from one select box based on value of other selectbox
(1 answer)
Closed 9 years ago.
I have 2 selects (a,b). Same data in both select.
I want that when I choose an option in one of them, the same option disapear in the second one. If a reselect an other option, the one tha disapeared should reappears
tx
check here http://jsfiddle.net/RTzwL/
html
<select id="first">
<option value="india">India</option>
<option value="usa">USA</option>
<option value="japan">Japan</option>
<option value="australia">Australia</option>
</select>
<select id="second">
<option value="india">India</option>
<option value="usa">USA</option>
<option value="japan">Japan</option>
<option value="australia">Australia</option>
</select>
Javascript
$('select').change(function() {
$(this).siblings('select').find('option').attr({disabled:false});
var thisIndex = parseInt($(this)[0].selectedIndex)+1;
$(this).siblings('select').find('option:nth-child('+thisIndex+')').attr({disabled:true});
});

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