2014-02-21 48 views
1

我目前拥有下面的代码(它是css和html的混合),用于切换。最终,用户可以切换到数据库中的值,从0更改为1.但是,我不知道如何在没有提交按钮的情况下执行此操作。用复选框更新MySQL值

CSS:

.onoffswitch { 
    position: relative; width: 45px; 
    -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 #E3E3E3; border-radius: 10px; 
} 
.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: 15px; padding: 0; line-height: 15px; 
    font-size: 7px; 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: 5px; 
    background-color: #5B969C; color: #EEEEEE; 
} 
.onoffswitch-inner:after { 
    content: "OFF"; 
    padding-right: 5px; 
    background-color: #EEEEEE; color: #5B969C; 
    text-align: right; 
} 
.onoffswitch-switch { 
    width: 15px; margin: 0px; 
    background: #FFFFFF; 
    border: 2px solid #E3E3E3; border-radius: 10px; 
    position: absolute; top: 0; bottom: 0; right: 26px; 
    -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; 
} 
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner { 
    margin-left: 0; 
} 
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch { 
    right: 0px; 
} 

HTML:

<div class="onoffswitch"> 
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox"  id="onoffswitch6"> 
    <label class="onoffswitch-label" for="onoffswitch6"> 
     <div class="onoffswitch-inner"></div> 
     <div class="onoffswitch-switch"></div> 
    </label> 
</div> 

编辑:的答案后,我加入这个脚本的页面和一个PHP文件。然而,它仅仅更改为1在数据库中,但不会改变其回0

脚本:

$(document).ready(function(){ 
      $('#onoffswitch6').change(function() { 
       var checked = $(this).is(':checked'); //true or false? 
       $.ajax({ 
        type: "post", 
        url: "encrypt.php", //PHP or whichever other language script that updates your database 
        data: { onOrOff : checked } //access this value in your script via $_POST['onOrOff'], if it's a PHP script 
        }); 
      }); 
     }); 

PHP文件:

<?php 
//Start session 
session_start(); 

//Include database connection details 
require_once('connection.php'); 

if(isset($_POST['onOrOff'])) 
{ 
    if ($_POST['onOrOff'] == false) { 
     global $onOrOff; 
     $onOrOff = 0; 
    } else if ($_POST['onOrOff'] == true) { 
     global $onOrOff; 
     $onOrOff = 1; 
    }; 

$qry = "UPDATE member SET value='$onOrOff' WHERE id='$_SESSION[SESS_MEMBER_ID]'"; 
$result = mysql_query($qry) or die("An error occurred ".mysql_error()); 
} 
?> 
+0

您可以使用该复选框的onChange处理Ajax请求,但你将不得不给我们多一些信息/语境。 – Overv

+0

@Overv你需要知道什么? – user3290485

+0

因此,当复选框被选中时,它会更新'值'列,但当用户取消选中时不更新? – lucasnadalutti

回答

1

AJAX是你所需要的。由于我没有太多的信息,我会做出一个小例子:

$('.onoffswitch-checkbox').change(function() { 
    var checked = $(this).is(':checked'); //true or false? 
    $.ajax({ 
     type: "post", 
     url: "myscript.php", //PHP or whichever other language script that updates your database 
     data: { onOrOff : checked } //access this value in your script via $_POST['onOrOff'], if it's a PHP script 
    }); 
}); 
+0

谢谢!还有一个问题,如果检查过,$ _POST ['onOrOff']会是什么? 1,检查,错误? – user3290485

+0

如果检查,它将是真实的。否则将是错误的。 – lucasnadalutti

+0

其实我错了,对不起。返回的值将被“检查”,如果被检查,则不是true。我会编辑我的答案,使其只能返回真或假,因为我认为这样更清洁。 – lucasnadalutti