使用jQuery来做,一个简单的例子可能是:
HTML:
<input type="checkbox" name="option1" value="Milk"><input type="checkbox" name="option2" value="Sugar"><input type="checkbox" name="option3" value="Chocolate">
JS:
$("input[type='checkbox']").on('click', function(){ var checked = $(this).attr('checked'); if(checked){ var value = $(this).val(); $.post('file.php', { value:value }, function(data){ // data = 0 - means that there was an error // data = 1 - means that everything is ok if(data == 1){ // Do something or do nothing :-) alert('Data was saved in db!'); } }); }});PHP:file.php
<?phpif ($_POST && isset($_POST['value'])) { // db connection $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { // error happened print(0); } mysql_select_db('mydb'); // sanitize the value $value = mysql_real_escape_string($_POST['value']); // start the query $sql = "INSERT INTO table (value) VALUES ('$value')"; // check if the query was executed if(mysql_query($sql, $link)){ // everything is Ok, the data was inserted print(1); } else { // error happened print(0); }}?>


