<?php
if($_POST){
foreach($_POST['id_user'] as $idUser){
mysql_query("delete * from auth_menu where id_user='$idUser'");//hapus data sebelumnya
foreach($_POST['grp'][$idUser] as $idGroup){
mysql_query("INSERT INTO `auth_menu` VALUES('$idGroup','$idUser','Checked')");
//tidak perlu mencatat checkbox yang tidak dicentang, karena jika tidak dicentang data tidak akan dikirim ke server
}
}
}
?>
<h2>Authorization User</h2>
<br><br>
<?php
$batas = 8;
$halaman = isset($_GET["halaman"]);
if (empty($halaman)) {
$posisi = 0;
$halaman = 1;
} else {
$posisi = ($halaman - 1) * $batas;
}
$sql = mysql_query("select * from viet_user order by id_user asc limit $posisi, $batas");
?>
<form method="POST" name="form1" action="">
<table border="1" cellspacing="0" cellpadding="4" noshade=noshade>
<tr>
<th style="background: #888888; color: #000000;">No</th>
<th style="background: #888888; color: #000000;">User Id</th>
<th style="background: #888888; color: #000000;">Name User</th>
<th style="background: #888888; color: #000000;">Menu Form</th>
</tr>
<?php
$no = 1;
while ($new = mysql_fetch_assoc($sql)) {?>
<tr>
<td valign="top"><?php echo $no; ?></td>
<td valign="top"><?php echo $new["id_user"]; ?></td>
<td valign="top"><?php echo $new["nama"]; ?></td>
<td>
<input type="hidden" name="id_user[]" value="<?php echo $new["id_user"]; ?>"/>
<?php
//cari auth_menu user bersangkutan
$userGroup = array();
$sqlUserGroup = mysql_query("select * from auth_menu where `id_user`='$new[id_user]'");
while($userGrp = mysql_fetch_array($sqlUserGroup)){
$userGroup[] = $userGroup['id_group'];
}
$sql_group = mysql_query("SELECT * FROM `group`");
while ($new1 = mysql_fetch_assoc($sql_group)) {
?>
<!-- jika user memiliki auth_menu tersebut centang checkbox -->
<input <?php echo in_array($new1['id_group'], $userGroup) ? 'checked="checked"' : ''; ?> type="checkbox" name="grp[<?php echo $new["id_user"]; ?>]" id="grp-<? echo $new1['id_group']; ?>" value="<? echo $new1['id_group']; ?>"/>
<!-- tampilkan nama group disini -->
<?php
}
?>
</td>
</tr>
<?php
$no++;
}
?>
</table>
<input type="submit" value="Submit"/>
</form>
tinggal tambahkan satu hidden field, menambahkan tombol submit, dan dengan penamaan checkbox yang benar tidak perlu lagi javascript