This tutorial shows how to get the value of multiple selected checkboxes that have the same value to the "name" attribute.
For example, if you have a list of articles, and you want to delete only the selected articles, add a checkbox to each article.
The trick is to add square brackets to the value of the "name" attribute, like this, to each checkbox:
In this way, when the form with the selected checkboxes is send to the PHP script, the variable: $_POST['thename'] will contain a numeric Array with the values of the selected checkboxes, having the index: 0, 1, 2, ...; in their order.
$_POST['thename'][0] = 'value of the first selected checkbox';
$_POST['thename'][1] = 'value of the second selected checkbox';

• Here's an example that you can test it.
// If data send via POST, with the name "cbname"
if(isset($_POST['cbname'])) {
  $cbarray = $_POST['cbname'];       // gets the value of the selected checkboxes
  print_r($cbarray);       // outputs the array with data stored in $cbarray
else {
  echo 'Select the web sites you like:';
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="cbname[]" value="coursesweb" /><br/>
<input type="checkbox" name="cbname[]" value="marplo" /><br/>
<input type="checkbox" name="cbname[]" value="php" /><br/>
<input type="checkbox" name="cbname[]" value="google" /><br/>
<input type="submit" value="Send" />

If you select all the checkboxes in the code above, the PHP script will output this result (the array with the values of the selected checkboxes, stored in $cbarray ):
Array ( [0] => coursesweb [1] => marplo [2] => php [3] => google )

- If the value of the checkboxes is the Unique ID of some articles selected from your database, and you want to delete the selected articles, you can use the implode() function to add the selected IDs (separated by comma) into a string that will be added in the SQL query. You must use the same technique to the name of the checkbox, adding the square brackets.
$ids = $_POST['cbname'];
$sql = "DELETE FROM `table_name` WHERE `id` IN(".implode(',' $ids).")";
- implode(',' $ids) will return a string like this:   "id1, id2, id3, ....".

Daily Test with Code Example

What attribute makes the input text box to not be modified by the user?
checked="checked" readonly="readonly" disabled="disabled"
<input type="text" value="fixed-value" readonly="readonly" name="a_name" />
What CSS property allows you to create rounded corners in your webpage design?
background-size border-size border-radius
.class {
  border:2px solid blue;
What instruction displays a confirmation dialog box to the viewer, who must then click OK or Cancel to proceed?
indexOf() confirm() prompt()
var ques = window.confirm("The result of 0+0 is 0?");
if (ques) alert("Corect");
else alert("Incorrect");
Indicate the PHP function that returns the lowest number of the parameter values.
floor() ceil() min()
$min_nr = min(12, 8, 25, 13);
echo $min_nr;        // 8
Get the value of multiple selected checkboxes with same name

Last accessed pages

  1. Button Symbols (832)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (104598)
  3. Convert JSON in JavaScript (5927)
  4. Draw arrow markers between clicks coords in Canvas (1379)
  5. Contact page - CoursesWeb (45936)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (359)
  2. Tabs effect with CSS (349)
  3. Contact page - CoursesWeb (348)
  4. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (129)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (73)