The HTML <select> element creates a drop-down list or list box. Use <option> elements for the available choices.
<label for="colour">Favourite colour</label>
<select id="colour" name="colour">
<option value="">Select a colour</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>
<select name="colours">
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>
The visible text inside each <option> is what the user sees. The value is what the browser submits when that option is selected.
| Attribute | Purpose |
|---|---|
name | Defines the submitted field name. |
id | Identifies the element and connects it to a label. |
multiple | Allows more than one option to be selected. |
size | Sets the number of visible rows in the list control. |
disabled | Disables the control and normally excludes it from form submission. |
required | Requires a valid selection for browser constraint validation. |
autofocus | Requests focus when the page loads. |
| Attribute | Purpose |
|---|---|
value | Defines the value submitted when the option is selected. |
selected | Makes the option selected when the page first loads. |
disabled | Makes the individual option unavailable for selection. |
label | Provides an alternate label for the option in supported contexts. |
A common pattern is to include an initial prompt option with an empty value:
<label for="colour2">Favourite colour</label>
<select id="colour2" name="colour" required>
<option value="" selected disabled>Select a colour</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>
Use the Boolean selected attribute to choose the initial option:
<select name="colour">
<option value="blue">Blue</option>
<option value="green" selected>Green</option>
<option value="red">Red</option>
</select>
With a normal single-select control, one option is selected at a time.
The old page used a non-standard width attribute on <select>. Use CSS instead.
<select name="colour" style="width: 350px;">
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>
For reusable layouts, place the width rule in a stylesheet instead of repeating an inline style.
Add the Boolean multiple attribute to allow more than one option to be selected.
<label for="colours_multi">Choose colours</label>
<select id="colours_multi" name="colours[]" size="4" multiple>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="red">Red</option>
<option value="yellow">Yellow</option>
<option value="white">White</option>
</select>
For PHP, the array-style name colours[] makes the selected values available as an array.
Handling multiple select values using PHP
The size attribute sets how many options are visible without scrolling.
<select name="colour" size="4">
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="red">Red</option>
<option value="yellow">Yellow</option>
</select>
A size greater than 1 generally displays the control as a list box instead of a compact one-line drop-down.
Use <optgroup> when a long select list contains meaningful groups.
<select name="vehicle">
<optgroup label="Cars">
<option value="sedan">Sedan</option>
<option value="suv">SUV</option>
</optgroup>
<optgroup label="Two wheelers">
<option value="bike">Bike</option>
</optgroup>
</select>
See OPTGROUP for grouping select options.
<select name="plan" disabled>
<option value="basic">Basic</option>
<option value="pro">Pro</option>
</select>
A disabled select is normally excluded from form submission.
<select name="plan">
<option value="basic">Basic</option>
<option value="enterprise" disabled>Enterprise</option>
</select>
<select name="country" autofocus>
<option value="in">India</option>
<option value="us">United States</option>
</select>
Use autofocus selectively because moving focus automatically can be disruptive for some users.
<form action="save.php" method="post">
<label for="country2">Country</label>
<select id="country2" name="country" required>
<option value="">Select a country</option>
<option value="in">India</option>
<option value="us">United States</option>
</select>
<button type="submit">Submit</button>
</form>
Browser validation can require a selection, but the receiving application must still validate the submitted value.
<?php
$allowed=[
'blue',
'green',
'red'
];
$colour=$_POST['colour'] ?? null;
if($colour === null || !in_array($colour, $allowed, true)){
echo 'Select a valid colour.';
}
<?php
$allowed=[
'blue',
'green',
'red',
'yellow',
'white'
];
$colours=$_POST['colours'] ?? [];
if(!is_array($colours)){
$colours=[];
}
$colours=array_values(
array_intersect($colours, $allowed)
);
See handling multiple select values in PHP.
Use CSS to control select width.
When an option has a value attribute, that value is submitted rather than the visible label.
When several options can be selected, the server must be prepared to receive several values. In PHP, names such as colours[] are convenient.
Keyboard and touch interactions vary by platform. Describe the task, not only one desktop key combination.
Use an empty value for prompts such as "Select a colour" and validate the submitted choice.
Disabled controls and disabled options are normally unavailable for normal submission.
Validate select values again on the server, preferably against an allowlist of accepted values.
Use a select element containing one or more option elements.
It defines the value submitted when that option is selected.
Add the selected Boolean attribute to that option.
Add the multiple attribute to select. The receiving application must then handle multiple submitted values.
It sets the number of visible option rows in the list control.
Use optgroup elements with a label attribute.
No. Use CSS for width and responsive layout.
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.
| sultan | 30-09-2014 |
| i want to select multiple values from drop-down list without using jquery or javascript as well as without pressing 'ctrl' or 'shift' key... please help... thanx in advance...... | |
| AravindAK | 30-12-2016 |
| Have to select multiple values in drop-down and also pass the value to next page with selected values...How to do??? | |
| smo1234 | 19-02-2017 |
| As explained above you can use multiple selection dropdown to give user choice to select more than one choice. On submission of the form it will be available in next page ( defined by action of the form ) and you can collect the inputs as an array by using PHP. | |
| suma | 13-02-2018 |
| i want to select multiple values from drop-down list at a time two multiple value select pls send the code html,jquery | |