 |
รบกวน ดู code ให้หน่อยครับ เกี่ยวกับ ทำ ปุ่ม ให้มี ค่า on off กับ เชฟค่า ครับ แยก ตาม fk |
|
 |
|
|
 |
 |
|
พอดีที่ผมทำมัน เชฟ แต่ ค่า on
จะ ทำ ให้ ปุ่ม เขียว เป็น on ปุ่ม แดงค่า off ทำอย่างไร ครับ
รบกวนช่วยทีครับ
ปุ่ม
Code (PHP)
<div class="onoffswitch">
<input type="checkbox" name="<?=$objResult['id']?>" class="onoffswitch-checkbox" id="<?=$objResult['id']?>" onchange="sper(this.value,<?=$objResult['id']?>,<?=$objResult['fk']?>)"<? 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>
js
Code (JavaScript)
<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
function sper(s,p,f)
{
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","opt/ajax-save-status.php?s="+ s +'&p='+p+'&f='+f,true);
/* postStr = "s="+ s +'&p='+ p +'&y=5';*/
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send();
}
//////////////////////////////////////////////////////////////
หน้าตัวเชฟ
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);
ขอบคุณล่วงหน้าคับ
Tag : PHP, JavaScript
|
|
 |
 |
 |
 |
Date :
2013-08-21 11:55:08 |
By :
nattaphong25332533 |
View :
1281 |
Reply :
10 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รบกวน ช่วยหน่อย ครับ
ตอนติก ออก (off) value มันไม่ off อะครับ   
|
 |
 |
 |
 |
Date :
2013-08-21 12:23:50 |
By :
nattaphong25332533 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณลืม <input type="checkbox" value=""> ลงไปนะครับ
|
 |
 |
 |
 |
Date :
2013-08-21 12:49:10 |
By :
Dragons_first |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใส่แล้ว มัน ก็ อ่าน ค่า แต่ on อะ ครับ ทำไง ให้ มัน ได้ค่า value = off อะ ครับ ตอน ติก ออก
|
 |
 |
 |
 |
Date :
2013-08-21 13:37:50 |
By :
nattaphong25332533 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณรับค่ามาจาก label นี่ครับ ไม่ได้รับค่ามาจาก checkbox ซะหนอ่ย แล้วมันจะเอาค่าไหนส่งมาให้ละครับ คุณต้องเปลี่ยนเป็นรับค่าจาก checkbox แทน
|
 |
 |
 |
 |
Date :
2013-08-21 14:15:07 |
By :
Dragons_first |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอโค๊ดทั้งหมดลองเอาไปรันคับ
|
 |
 |
 |
 |
Date :
2013-08-21 14:15:48 |
By :
Dragons_first |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Css
Code
<style type="text/css">
.onoffswitch {
position: relative; width: 90px;
-webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block; overflow: hidden; cursor: pointer;
border: 2px solid #666666; border-radius: 5px;
}
.onoffswitch-inner {
width: 200%; margin-left: -100%;
-moz-transition: margin 0.3s ease-in 0s; -webkit-transition: margin 0.3s ease-in 0s;
-o-transition: margin 0.3s ease-in 0s; transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
float: left; width: 50%; height: 30px; padding: 0; line-height: 30px;
font-size: 16px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
-moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;
}
.onoffswitch-inner:before {
content: "ON";
padding-left: 10px;
background-color: #00C707; color: #FFFFFF;
}
.onoffswitch-inner:after {
content: "OFF";
padding-right: 10px;
background-color: #FF0000; color: #666666;
text-align: right;
}
.onoffswitch-switch {
width: 35px; margin: 0px;
background: #FFFFFF;
border: 2px solid #666666; border-radius: 5px;
position: absolute; top: 0; bottom: 0; right: 51px;
-moz-transition: all 0.3s ease-in 0s; -webkit-transition: all 0.3s ease-in 0s;
-o-transition: all 0.3s ease-in 0s; transition: all 0.3s ease-in 0s;
background-image: -moz-linear-gradient(center top, rgba(0,0,0,0.1) 0%, rgba(0,0,0,0) 100%);
background-image: -webkit-linear-gradient(center top, rgba(0,0,0,0.1) 0%, rgba(0,0,0,0) 100%);
background-image: -o-linear-gradient(center top, rgba(0,0,0,0.1) 0%, rgba(0,0,0,0) 100%);
background-image: linear-gradient(center top, rgba(0,0,0,0.1) 0%, rgba(0,0,0,0) 100%);
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
right: 0px;
}
</style>
หน้า ที่มีปุ่ม
Code (PHP)
<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);
|
 |
 |
 |
 |
Date :
2013-08-21 15:07:02 |
By :
nattaphong25332533 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันส่ง ค่า on อย่าง เดียว เลย ครับ จะ ทำ ไง ตอน กด off แดง ส่ง off ไป อะ ครับ   
|
 |
 |
 |
 |
Date :
2013-08-21 15:09:18 |
By :
nattaphong25332533 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
หน้า update ตอนรับค่าก็ให้เช็คสถานะดูอีกทีก่อนจะบันทึกลงฐานข้อมูลก็ได้ครับ
if($_GET['s']=='on'){
$status='on';
}else{
$status='off';
}
$sql = "UPDATE tb_ban SET status = '$status' ";
$sql .= "WHERE id = '".$_GET['p']."' and fk = '".$_GET['f']."' " ;
$update = mysql_query($sql) or die($sql);
|
 |
 |
 |
 |
Date :
2013-08-21 15:57:12 |
By :
phpbasic |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
  
ขอบคุน ครับ
|
 |
 |
 |
 |
Date :
2013-08-21 16:31:32 |
By :
nattaphong25332533 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|