我在链接上尝试了完全相同的代码: - http://www.freezecoders.com/2013/05/store-value-from-onoff-button-in-mysql-database-using-php-ajax.html
Index.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>
这个ajax.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>";
}
?>
但事实是它不适用于我的wamp服务器。这段代码会引发很多错误,例如:不推荐使用:mysql_connect():不推荐使用mysql扩展,将来会删除它:在第2行的C:\ wamp \ www \ btn \ index.php中使用mysqli或PDO 和: - 第109行的mysqli_fetch_array()错误。给定代码有什么问题?有没有人可以帮我这个?
答案 0 :(得分:1)
答案已经在您在问题中发布的错误中。 你正在使用旧的mysql方法,这些方法在新的php中已被弃用,因此你必须使用mysqli(改进的mysql)方法。
使用正确的语法将mysqli命令替换为所有mysql ..如果需要,请在线检查mysqli文档。
例如 - 用mysqli_connect和其他mysql命令替换mysql_connect ..
我希望它能解决你的问题。
答案 1 :(得分:-1)
<input type="checkbox" id="<?=$row['id']?>" name="onoffswitch" value="<?=$row['status']?>" class="js-switch" <?=$row['status'] == '1' ? 'checked' : '' ;?>/>
$('input[name=onoffswitch]').click(function(){
var id=$(this).attr('id');
var status = $(this).val();
if(status == 1) {
status = 0;
} else {
status = 1;
}
//alert(id);
$.ajax({
type:'POST',
url:'updateustaus.php',
data:'id= ' + id + '&status='+status
});
});