Store Value from On/Off Button in MySQL Database Using PHP Ajax

By | May 23, 2013

In most of the applications and in registration form, instead of using checkbox/radio button, animated on/off button being used to make user experience more user interface. Here is the tutorial for Store Value from On/Off Button in MySQL Database Using PHP Ajax. Selected value either it may be on/off, it will be stored in database.

Get your stylish ON/OFF button here

Database

create a table with following fields

id

name

choice

CSS & jQuery for ON/OFF Button

[code type=html]

<script type=”text/javascript”>
$(document).ready( function(){
$(“.cb-enable”).click(function(){
var parent = $(this).parents(‘.switch’);
$(‘.cb-disable’,parent).removeClass(‘selected’);
$(this).addClass(‘selected’);
$(‘.checkbox’,parent).attr(‘checked’, true);
});
$(“.cb-disable”).click(function(){
var parent = $(this).parents(‘.switch’);
$(‘.cb-enable’,parent).removeClass(‘selected’);
$(this).addClass(‘selected’);
$(‘.checkbox’,parent).attr(‘checked’, false);
});
});
</script>

<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 #999999; border-radius: 20px;
}
.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: 14px; 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: “OFF”;
padding-left: 10px;
background-color: #2FCCFF; color: #FFFFFF;
}
.onoffswitch-inner:after {
content: “ON”;
padding-right: 10px;
background-color: #EEEEEE; color: #999999;
text-align: right;
}
.onoffswitch-switch {
width: 18px; margin: 6px;
background: #FFFFFF;
border: 2px solid #999999; border-radius: 20px;
position: absolute; top: 0; bottom: 0; right: 56px;
-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;
}
</style>

[/code]

Full Coding for ON/OFF Button with PHP Ajax

When we clicked on/off button, jquery script will be triggered and value will be passed to ajax.php. Then ajax.php will update the value in database.

Following code will remain the button value as same as in database value. If we selected value as “OFF”, when we reopen the webpage/refresh the webpage  the value will be remained as OFF.

 <input type=”checkbox” name=”onoffswitch” id=”myonoffswitch”
<?php
$query3=mysql_query(“select * from choice where id=1″);
$query4=mysql_fetch_array($query3);
if($query4[‘choice’]==”off”)
{
echo “checked”;
}
?>>

index.php

[code type=php]

<?php
$query=mysql_connect(“localhost”,”root”,””);
mysql_select_db(“freeze”,$query);
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head profile=”http://gmpg.org/xfn/11″>
<title>iPhone Style Radio and Checkbox Switches, found on DevGrow.com</title>
<script type=”text/javascript”src=”http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js”>
</script>
<script type=”text/javascript”>
$(document).ready(function(){
$(‘#myonoffswitch’).click(function(){
var myonoffswitch=$(‘#myonoffswitch’).val();
if ($(“#myonoffswitch:checked”).length == 0)
{
var a=myonoffswitch;
}
else
{
var a=”off”;
}

$.ajax({
type: “POST”,
url: “ajax.php”,
data: “value=”+a ,
success: function(html){
$(“#display”).html(html).show();
}
});

});
});
</script>

<script type=”text/javascript”>
$(document).ready( function(){
$(“.cb-enable”).click(function(){
var parent = $(this).parents(‘.switch’);
$(‘.cb-disable’,parent).removeClass(‘selected’);
$(this).addClass(‘selected’);
$(‘.checkbox’,parent).attr(‘checked’, true);
});
$(“.cb-disable”).click(function(){
var parent = $(this).parents(‘.switch’);
$(‘.cb-enable’,parent).removeClass(‘selected’);
$(this).addClass(‘selected’);
$(‘.checkbox’,parent).attr(‘checked’, false);
});
});
</script>

<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 #999999; border-radius: 20px;
}
.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: 14px; 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: “OFF”;
padding-left: 10px;
background-color: #2FCCFF; color: #FFFFFF;
}
.onoffswitch-inner:after {
content: “ON”;
padding-right: 10px;
background-color: #EEEEEE; color: #999999;
text-align: right;
}
.onoffswitch-switch {
width: 18px; margin: 6px;
background: #FFFFFF;
border: 2px solid #999999; border-radius: 20px;
position: absolute; top: 0; bottom: 0; right: 56px;
-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;
}
</style>

</head>
<body>
<div class=”onoffswitch”>
<input type=”checkbox” name=”onoffswitch” class=”onoffswitch-checkbox” id=”myonoffswitch”
<?php
$query3=mysql_query(“select * from choice where id=1″);
$query4=mysql_fetch_array($query3);
if($query4[‘choice’]==”off”)
{
echo “checked”;
}
?>>
<label class=”onoffswitch-label” for=”myonoffswitch”>
<div class=”onoffswitch-inner”></div>
<div class=”onoffswitch-switch”></div>
</label>
</div>

<div id=”display”></div>
</body>
</html>

[/code]

 

ajax.php

[code type=php]
<?php
$query=mysql_connect(“localhost”,”root”,””);
mysql_select_db(“freeze”,$query);
if(isset($_POST[‘value’]))
{
$value=$_POST[‘value’];
mysql_query(“update choice set choice=’$value’ where id=’1′”);
echo “<h2>You have Chosen the button status as:” .$value.”</h2>”;
}
?>

[/code]

14 thoughts on “Store Value from On/Off Button in MySQL Database Using PHP Ajax

  1. John

    This is just what i needed, but I have a question:
    The db name is freezy, but what about the table name?
    I cant create the fields without creating a table first..

    Reply
  2. asif

    Nice one. But what if id is also a variable.
    i,e instead of id=1 some id=’$id?

    Reply
  3. sheeraz kaleem

    if we apply multiple check boxes then how CSS apply.
    Please explain in detail.
    Thanks in Advance.

    Reply
  4. Casper C.

    Hello I really enjoy’ed your guide. 🙂 Works like a charm. If you got some spare time I would love to know how to add more buttons ?

    Reply

Leave a Reply to Abhilash Raj R S Cancel reply

Your email address will not be published. Required fields are marked *