$sql = "UPDATE tb_ban SET status = '".$_GET['s']."' ";
$sql .= "WHERE id = '".$_GET['p']."' and fk = '".$_GET['f']."' " ;
$update = mysql_query($sql) or die($sql);
<script>
function sper(s,p)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//document.getElementById("txtlist").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax-save-status.php?s="+ s +'&p='+p,true);
/* postStr = "s="+ s +'&p='+ p +'&y=5';*/
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send();
}
</script>
//sql
$strSQL = "SELECT * FROM tb_ban where fk = 1 ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
// ส่วน customize table
$num=1;
while($objResult = mysql_fetch_array($objQuery))
{
//ปุ่ม
<div class="onoffswitch">
<input type="checkbox" name="<?=$objResult['id']?>" class="onoffswitch-checkbox" id="<?=$objResult['id']?>" value="on" onchange="sper(this.value,<?=$objResult['id']?>)"<? if($objResult['status']=='on')echo 'checked';?> >
<label class="onoffswitch-label" for="<?=$objResult['id']?>" >
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
<?php $num++; } ?>
<script type="text/javascript">
$(document).ready(function(){
var switchBoard = $("label.onoffswitch-label");
var switchDiv = $("div.onoffswitch");
var checkedState,currentSate;
switchBoard.on("click",function(){
checkState = $(this).prev(":checkbox").attr("checked");
currentState = $(this).parents("div").index();
switchDiv.each(function(i){
if(i == currentState){
}else{
if($("input:checkbox",this).attr("checked") == checkState && checkState == "checked")
$("input:checkbox",this).attr("checked","checked");
else
$("input:checkbox",this).removeAttr("checked");
}
});
});
});
</script>
หน้าที่ ajax-save-status.php
Code (PHP)
$sql = "UPDATE tb_ban SET status = '".$_GET['s']."' ";
$sql .= "WHERE id = '".$_GET['p']."' and fk = '".$_GET['f']."' " ;
$update = mysql_query($sql) or die($sql);
$sql = "UPDATE tb_ban SET status = '$status' ";
$sql .= "WHERE id = '".$_GET['p']."' and fk = '".$_GET['f']."' " ;
$update = mysql_query($sql) or die($sql);