Breaking News
Home / Web / HTML / How do create multi select checkboxes in HTML?

How do create multi select checkboxes in HTML?

Let’s say you have html form where in you can accept multiple values for a same field and the GUI has to be presented as a series of checkboxes the user can check/uncheck.

This is done  by using the array index operator ‘[]’ to the name attribute of the checkbox element as show below:

<form method=”POST” action=”…”>
Maths <input type=”checkbox” name=”subject[]” value=”math” />
Science <input type=”checkbox” name=”subject[]” value=”science” />
English <input type=”checkbox” name=”subject[]” value=”english” />
Social <input type=”checkbox” name=”subject[]” value=”social” />

</form>

Lets say we are using PHP for the serverside, then we can use the

$_POST[‘subject’] variable to get all the values that were selected by the user as an array. For example of all these checkboxes were selected then the output will be.

print_r($_POST[‘subject’]);

// output
Array
(
    [0] => math
    [1] => science
    [2] => english
    [3] => social )

[pb_builder]

Check Also

Coolest Things in HTML5

HTML (hypertext markup language) is very used markup language for the building the web pages. …

Advertisment ad adsense adlogger