Jump to: navigation, search

PHP HTML Dynamic Checkbox From Database

From w3cyberlearnings

Contents

PHP Dynamic Checkbox From Database

Make HTML checkbox from database.

Table: student

CREATE TABLE student
(
student_id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
PRIMARY KEY(student_id)
);

Records

NSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (1,'Johny','Zhar');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (2,'Jacob','Milli');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (3,'lili','lee');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (4,'Michael','Vieva');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (5,'Emma','lee');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (6,'Peeou','Zhou');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (7,'Isabel','Peom');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (8,'Sophal','Tang');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (9,'Chong','Mok');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (10,'Ebo','Kaun');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (11,'salina','lee');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (12,'pheank','so');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (13,'lyekwan','lee');
INSERT INTO `student` (`student_id`,`first_name`,`last_name`) VALUES (14,'kiloma','otam');

Example 1

<?php

define('HOST', 'localhost');
define('USER', 'root');
define('PASS', 'yeething');
define('DBNAME', 'woowood');

$db = new mysqli(HOST, USER, PASS, DBNAME);

if ($db->connect_errno) {
	echo "Failed to connect to MySQL: (" . $db->connect_errno . ") "
	. $db->connect_error;
} else {
	$sql = "SELECT student_id, concat(first_name,' ',last_name) as name FROM student";
	$result_db = $db->query($sql);
	if (!$result_db) {
		echo $db->error . ' Error perform query!';
	} else {

		while ($row = $result_db->fetch_object()) {
	          echo "<input type=\"checkbox\" 
                         name=\"student[]\" 
                        value=\"{$row->student_id}\" id=\"id{$row->student_id}\"/>";

		  echo "<label for=\"id{$row->student_id}\">{$row->name}</label><br/>";
		}
	}
}
$db->close();
?>

Output: HTML Source


<input type="checkbox" name="student[]" value="1" id="id1"/>
<label for="id1">Johny Zhar</label><br/>
<input type="checkbox" name="student[]" value="2" id="id2"/>
<label for="id2">Jacob Milli</label><br/>
<input type="checkbox" name="student[]" value="3" id="id3"/>
<label for="id3">lili lee</label><br/>
<input type="checkbox" name="student[]" value="4" id="id4"/>
<label for="id4">Michael Vieva</label><br/>
<input type="checkbox" name="student[]" value="5" id="id5"/>
<label for="id5">Emma lee</label><br/>
<input type="checkbox" name="student[]" value="6" id="id6"/>
<label for="id6">Peeou Zhou</label><br/>
<input type="checkbox" name="student[]" value="7" id="id7"/>
<label for="id7">Isabel Peom</label><br/>
<input type="checkbox" name="student[]" value="8" id="id8"/>
<label for="id8">Sophal Tang</label><br/>
<input type="checkbox" name="student[]" value="9" id="id9"/>
<label for="id9">Chong Mok</label><br/>
<input type="checkbox" name="student[]" value="10" id="id10"/>
<label for="id10">Ebo Kaun</label><br/>
<input type="checkbox" name="student[]" value="11" id="id11"/>
<label for="id11">salina lee</label><br/>
<input type="checkbox" name="student[]" value="12" id="id12"/>
<label for="id12">pheank so</label><br/>
<input type="checkbox" name="student[]" value="13" id="id13"/>
<label for="id13">lyekwan lee</label><br/>
<input type="checkbox" name="student[]" value="14" id="id14"/>
<label for="id14">kiloma otam</label><br/>

Related Links


Dynamic HTML From Array
  1. Dynamic Drop Down List with Array
  2. Dynamic List with Array
  3. Dynamic Radio with Array
  4. Dynamic Checkbox with Array
Dynamic HTML From Database
  1. Dynamic Drop Down List with Database
  2. Dynamic List from Database
  3. Dynamic Radio from Database
  4. Dynamic Checkbox from database
Dynamic HTML with Ajax
  1. Dynamic Drop Down List with Ajax
  2. Dynamic List with Ajax
  3. Dynamic Radio with Ajax
  4. Dynamic Checkbox with Ajax
  5. Dynamic Form with Ajax
Others Related
  1. Dynamic Drop down with default option
  2. How to retrieve Form Data
Navigation
Web
SQL
MISC
References