I have multiple elements with checkboxes; on selecting a box, I want to store some of its attributes passed via the ajax data parameter into a database table. Here's an example of the JS and html markup.
$("input[type="submit"]").live("change", function() {
if($(this).is(":checked")) {
$.ajax({
type: 'post',
url: 'fetch.php',
data:
{
id: $(this).attr('id'),
checked: 'yes'
}
})
}
})
<li>
<label for="one"></label>
<input type="checkbox" name="one" id="one">
</li>
<li>
<label for="two"></label>
<input type="checkbox" name="two" id="two">
</li>
<li>
<label for="three"></label>
<input type="checkbox" name="three" id="three">
</li>
However, as a PHP and mysql newbie, after succesfully connecting to my mysql table, I got stuck on the actual PHP code. The following doesn't do anything and I'm struggling to proceed.
<?php
if(isset($_POST['id'])){
if (is_array($_POST['id'])) {
foreach($_POST['id'] as $value){
$query = "INSERT INTO my_table VALUES ". $value. ";";
mysql_query($query) or die(mysql_error());
}
} else {
echo "nothing checked";
}
}
?>
Any help would be greatly appreciated.