You can realize it by biding onchange event handler to checkbox (with
JavaScript).
When checkbox is changed, you generated information about a change. That information would be set to PHP file (using
AJAX method).
Inside PHP file you would receive that information, use it to determine what action to take (add/delete), and with which item to perform it.
Here is example of
AJAX using jQuery plugin:
===========================================
HTML:
-------------------------------------------------------
- Code: Select all
<html>
<head>
<title>AJAX</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(window).ready(function() {
// add event
$('.checkbox').change(function() {
// get checkbox value
var item_id = $(this).val();
// get checkbox status (checked/not)
if(this.checked == true)
var to_do = "add";
else
var to_do = "delete";
// perform AJAX
$.ajax({
// data transfer method
type: "POST",
// url to request
url: "duh.php",
// data to transfer
data: { action: to_do, id: item_id },
// what to do on successful connection
success: function(html_came_back) {
alert(html_came_back);
}
});
});
});
</script>
</head>
<body>
<input type="checkbox" class="checkbox" name="vehicle" value="1" /> bike<br />
</body>
</html>
===========================================
duh.php
-------------------------------------------------------
- Code: Select all
<?php
$action = $_POST['action'];
$id= $_POST['id'];
switch($action) {
case 'add':
echo "add this ".$id;
break;
case 'delete':
echo "delete this ".$id;
break;
}
?>