You should avoid using mysql_*
functions since they are deprecated from PHP 5.5.x and will be removed in the future.
I really suggest moving towards PDO as it is more OOP or if you prefer the old procedural way then mysqli is the way to go.
Now, with that said:
Lets start with the html/javascript part:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title be here</title>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
</head>
<body>
<form action="" method="post" id="_form">
<div class="row">
<?php for($i = 1; $i <= 10; $i++):?>
<label for="id_<?php echo $i; ?>">Checkbox <?php echo $i; ?></label>
<input type="checkbox" id="id_<?php echo $i; ?>" name="ids[<?php echo $i; ?>]"><!-- here we assume $i is $id -->
<br />
<?php endfor; ?>
<input type="submit" value="Submit" name="submit" id="_submit">
</div>
</form>
<script type="text/javascript">
$(document).ready(function(){
$('#_submit').on('click', function(e){
e.preventDefault();
$.ajax({
type: "post",
url: "file.php",
data: $('#_form').serialize(),
success: function(data){
if(data.success == true){
// do stuff here
}else{
// do other stuff here
}
}
});
});
});
</script>
</body>
</html>
And lets continue with the PHP part:
<?php
if(isset($_POST['ids']))
{
$input = $_POST['ids'];
// Lets take care of the query here
$query = ' UPDATE `article` SET `publish` = CASE';
foreach($input as $key => $value)
{
$query .= ' WHEN `id` = ' . /* typecast to int just in case */ (int) $key . ' THEN ';
if($value == 'on')
{
$query .= '1';
}
else
{
$query .= '0';
}
}
$query .= ' END WHERE `id` IN (' . implode(',', array_keys($input)) . ')';
// update stuff using the database handler here
// return a json response here or just echo out whatever you need
}
else
{
// return a response here
}
Notice that I've placed name="ids[<?php echo $i; ?>]"
as an array. Php will actually interpret that as an array, and it will look like this:
Array
(
[2] => on
[4] => on
[5] => on
[6] => on
[7] => on
[8] => on
)
Note that the keys are not random, they are our ids.
However, you'll also note that some ids are missing, namely the ones that didn't have the checkbox checked. That would be a problem. A simple workaround would be to do the following:
<input type="hidden" name="ids[<?php echo $i; ?>]" value="off">
<!-- Add a hidden filed with the exact same id BUT with off as a value -->
<!-- In case the user checks the checkbox, then it will overwrite the hidden field -->
<!-- The order of the fields in very important here. Hidden field MUST come first -->
<input type="checkbox" id="id_<?php echo $i; ?>" name="ids[<?php echo $i; ?>]"><!-- here we assume $i is $id -->
So now our array looks like this:
Array
(
[1] => off
[2] => off
[3] => off
[4] => off
[5] => on
[6] => off
[7] => off
[8] => on
[9] => off
[10] => off
)
From there on it's a simple matter of building the query and updating the table.
The query will look something like this:
UPDATE `article` SET `publish` = CASE WHEN `id` = 4 THEN 1 WHEN `id` = 5 THEN 1 WHEN `id` = 6 THEN 1 END WHERE `id` IN (4,5,6)
Which will allow you to update all the records in one go without looping over the results and without being forced to execute one query for each checkbox.
As for the front-end part as the user experience, I suppose you could simply flash an alert when the update is done and successful, otherwise just display an error.