When the check box is selected,the database will be updated.The field value of "checked" is set to '1' when the check box is selected.If the check box is deselected, the value will be '0'.

The database needs to be set up first.The following is the sql export I used as my table.

sql

CREATE TABLE IF NOT EXISTS `check` (

`id` int(32) NOT NULL AUTO_INCREMENT,

`name` text NOT NULL,

`checked` int(5) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;



--

-- Dumping data for table `check`

--



INSERT INTO `check` (`id`, `name`, `checked`) VALUES

(1, 'Nimesha', NULL),

(2, 'Savinda', NULL),

(3, 'Priyangi', NULL),

(4, 'Heshani', NULL);


index.php

<?php

$host       = "localhost";

$username   = "root";

$password   = "root";

$database   = "site";

$table      = "check";



$link = mysql_connect($host, $username, $password)or die('Could not connect: ' . mysql_error());

$db_selected = mysql_select_db($database, $link);



$sql = "SELECT * FROM $table";

$result = mysql_query($sql);

?>



<html>

<head>

<script type="text/JavaScript" src="index.js"></script>

</head>

<title>Update database when selecting/deselecting checkbox</title>

<body>

<table align="center">

<tbody>



<?php

while ($row = mysql_fetch_assoc($result)) {

?>



<tr>

<td width="60"><input type="checkbox" name="check[]" id="" onChange="check(this)" value="<?php echo $row['id']; ?>"  /></td>

<td width="100"><?php  echo $row['id']; ?></td>

<td width="100"><?php  echo $row['name']; ?></td>

</tr>

<?php } ?>



</tbody>

</table>

</body>

</html>


index.js

function ajax(){

var ajaxRequest;  // The variable that makes Ajax possible!

try{

// Opera 8.0+, Firefox, Safari

ajaxRequest = new XMLHttpRequest();

} catch (e){

// Internet Explorer Browsers

try{

ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");

} catch (e) {

try{

ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");

} catch (e){

// Something went wrong

alert("Your browser broke!");

return false;

}

}

}

// Create a function that will receive data sent from the server

ajaxRequest.onreadystatechange = function(){

if(ajaxRequest.readyState == 4){

}

}

return ajaxRequest;



}



function check(sender){

var ajaxRequest = this.ajax();

if(sender.checked == true){

var mark = 1;

}

else{

mark = 0;

}

var ID = sender.value;

var queryString = "?id=" + ID+"&mark=" +mark;

ajaxRequest.open("GET", "check.php" + queryString, true);

ajaxRequest.send(null);

}


check.php

<?php

$id = $_GET['id'];

$mark = $_GET['mark'];



$host       = "localhost";

$username   = "root";

$password   = "root";

$database   = "site";



$link = mysql_connect($host, $username, $password)or die('Could not connect: ' . mysql_error());

$db_selected = mysql_select_db($database, $link);



$sqlUpdate = "UPDATE check SET checked='$mark' WHERE id='".$id."'";

$resultUdate = mysql_query($sqlUpdate);