2014-02-21 105 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