Use jQuery to read, add, remove, reset and submit options in a dropdown/listbox. Native <select> elements work well for single or multiple selection.
const value =
$( "#listbox" ).val();
const text =
$( "#listbox option:selected" )
.text();
$( "#listbox" ).on(
"change",
function () {
$( "#result" ).text(
$( this ).val()
);
}
);
$( "#listbox" )
.prop(
"selectedIndex",
0
);
$( "#listbox" )
.prop( "disabled", true );
$( "#listbox" )
.prop( "disabled", false );
$( "<option>", {
value: "new-value",
text: "New option"
}).appendTo( "#listbox" );
$( "#listbox option:selected" )
.remove();
$( "#listbox" ).empty();
<select id="listbox"
name="items[]"
multiple>
</select>
const selected =
$( "#listbox" ).val() || [];
$( "#listbox option" )
.prop( "selected", true );
With name="items[]", PHP receives an array of selected values. Validate every submitted value before using it in a query.
$items = $_POST['items'] ?? [];
if (!is_array($items)) {
$items = [];
}
Populate options from MySQL/PDO exactly as in the database dropdown tutorial. See PDO records and MySQLi SELECT.
Download original listbox examples
Redirect based on listbox selection →
var sel = $("#student").val();var sel = $('#student option:selected').text()<script>
$(document).ready(function() {
$("#student").change(function(){ // change function of listbox
var sel = $("#student").val(); // reading listbox selection
$("#t1").attr('value',sel); // keeping in a text box
});
});
</script>$("#student1,#student2,#student3").change(function(){
......
});$(".my_class").change(function(){ // change function of listbox
var var_value = $(this).val(); // reading listbox selection value
var var_id = sel + $(this).attr('id'); // reading id of selected listbox
});$("#student").prop('selectedIndex',0);Student <select id=student>
<option value='Alex'>Alex</option>
<option value='Raju'>Raju</option>
<option value='Raman'>Raman</option>
</select> <br><br>
<input type=text name=t1 id=t1><select name=cat_id id=cat_id class='form-control'>
....
</select><select id=student class='form-control'>
..
</select><select id=student class='form-control' style='width:auto;'>
..
</select><select id=student class='form-control' style='width:100px'>
..
</select><select id=student class='form-control' style='width:75%'>
..
</select>$sql='select * from student ';
echo "<select id=name name=name class='form-control'>";
foreach ($dbo->query($sql) as $row) {
echo "<option value=$row[id]>$row[name]</option>";
}
echo "</select>";if($stmt = $connection->query("SELECT * from student")){
echo "<select id=name name=name class='form-control'>";
while ($row = $stmt->fetch_assoc()) {
echo "<option value=$row[id]>$row[name]</option>";
}
echo "</select>";
}else{
echo $connection->error;
}$('#student1').attr('disabled', true);$('#student1').prop('disabled',false);$('#student1,#student2').prop('disabled',false);$("#student").append("<option value=" + to_add +">"+to_add+"</option>");<script>
$(document).ready(function() {
$("#b1").click(function(){
var to_add = $("#t1").val();
$("#student").append("<option value=" + to_add +">"+to_add+"</option>");
$("#t1").val('');
});
});
</script>
Add text and submit to enter as option<br><br>
Student <select id=student>
<option value='Alex'>Alex</option>
<option value='Raju'>Raju</option>
</select> <br><br>
<input type=text name=t1 id=t1>
<input type=button name=b1 id=b1 value=Add>var to_remove = $("#student").val();$("#student option[value= '"+ to_remove + "' ]").remove();<script>
$(document).ready(function() {
$("#b1").click(function(){
var to_remove = $("#student").val();
$("#student option[value= '"+ to_remove + "' ]").remove();
});
});
</script>
Select One option to remove / add.<br><br>
Student <select id=student>
<option value='Alex'>Alex</option>
<option value='Raju'>Raju</option>
<option value='Rone'>Rone</option>
<option value='Peter'>Peter</option>
</select>
<input type=button name=b1 id=b1 value=Remove><script>
$(document).ready(function() {
$("#b1").click(function(){
$("#student").empty();
});
});
</script>
Select One option to remove / add.<br><br>
Student <select id=student>
<option value='Alex'>Alex</option>
<option value='Raju'>Raju</option>
<option value='Rone'>Rone</option>
<option value='Peter'>Peter</option>
</select>
<input type=button name=b1 id=b1 value='Remove All'>$("#student").change(function(){
//var str = ($("#student").val() || []).join(', ');
var str=new Array();
$("#student option:selected").each(function() {
str.push($(this).val());
});$("#display1").html(str.join(","));$("#b2").click(function(){
$('#student option').prop('selected', true);
});<script>
$(document).ready(function() {
$("#student").change(function(){
//var str = ($("#student").val() || []).join(', ');
var str=new Array();
$("#student option:selected").each(function() {
str.push($(this).val());
});
$("#display1").html(str.join(","));
});
////////
$("#b2").click(function(){
$('#student option').prop('selected', true);
});
///////
});
</script>
Select Multiple options by pressing Ctrl key<br><br>
Selected options will be displayed. <br><br>
<form method=post action=listbox5ck.php>
<select id=student size=4 multiple name=student[]>
<option value='Alex'>Alex</option>
<option value='Raju'>Raju</option>
<option value='Rone'>Rone</option>
<option value='Peter'>Peter</option>
<option value='Ramu'>Ramu</option>
<option value='John'>John</option>
</select><input type=button name=b2 id=b2 value='Select All'><br>
<br><br><input type=submit name=b1 id=b1 value='Submit'>
<div id=display1></div><?Php
echo "Your selections are here <br><br>";
$str=$_POST['student'];
while (list ($key, $val) = each ($str)) {
echo "$key -> $val <br>";
}
?>if( is_array($student) ){
$sql_student='(';
while (list ($key, $val) = each ($student)) {
//echo "$val <br>";
if(strlen($val)>0){
$sql_student = $sql_student."'".$val."'".",";
}
}
$sql_student=substr($sql_student,0,(strLen($sql_student)-1));
$sql_student=$sql_student.")";
$sql_student= " AND my_student_table.student IN $sql_student ";
}
else{$sql_student='';}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
15-09-2020 | |
| This is an amazing tutorial! Thank you very much, it was very beneficial to me. | |