Php-mysql Course

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:
name="thename[]"
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.
<?php
// 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:';
}
?>
<br/><br/>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="cbname[]" value="coursesweb" />https://coursesweb.net<br/>
<input type="checkbox" name="cbname[]" value="marplo" />marplo.net<br/>
<input type="checkbox" name="cbname[]" value="php" />www.PHP.net<br/>
<input type="checkbox" name="cbname[]" value="google" />www.Google.com<br/>
<input type="submit" value="Send" />
</form>

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.
Example:
$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

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Get the value of multiple selected checkboxes with same name

Last accessed pages

  1. Get Mime Type of file or string content in PHP (6230)
  2. Countdown Timer with starting time added into a form (11533)
  3. Disable button and Enable it after specified time (17533)
  4. JpGraph - Create Graph, Charts, Plots in PHP (3933)
  5. Simple Admin Login PHP Script (10999)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (71)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)