Php 复选框状态:使用setinterval和mysql数据库刷新Ajax
我有一个复选框,每次单击它,状态都会记录到MySQL数据库中。 如何从数据库中获取状态值,每5秒刷新一次复选框的状态(使用Ajax-setInterval) 目前,代码如下所示:Php 复选框状态:使用setinterval和mysql数据库刷新Ajax,php,mysql,ajax,checkbox,Php,Mysql,Ajax,Checkbox,我有一个复选框,每次单击它,状态都会记录到MySQL数据库中。 如何从数据库中获取状态值,每5秒刷新一次复选框的状态(使用Ajax-setInterval) 目前,代码如下所示: <div class="onoffswitch"> <input type="hidden" id="hidden1" value="6"> <input type="checkbox" name="onoffswitch1" class="onoffswitch-checkbox" id
<div class="onoffswitch">
<input type="hidden" id="hidden1" value="6">
<input type="checkbox" name="onoffswitch1" class="onoffswitch-checkbox" id="myonoffswitch1"
<?php
$query3=mysql_query("select * from choice where id=6");
$query4=mysql_fetch_array($query3);
if($query4['choice']=="1")
{
echo "checked";
}
?>>
<label class="onoffswitch-label" for="myonoffswitch1">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
您可以按时间间隔设置ajax进程。在使用该代码之前,请确保页面中已包含jQuery库。因此:
<script type="text/javascript">
setInterval(function(){
$.ajax({
type: 'POST', /* Possible GET */
data: {...},
url: 'file.php' /* Relative path to file */
success: function(response){
/* do what you wabt with response object **/
}
});
},5000);
</scritp>
setInterval(函数(){
$.ajax({
键入:'POST',/*可能获取*/
数据:{…},
url:'file.php'/*文件的相对路径*/
成功:功能(响应){
/*对响应对象执行所需操作**/
}
});
},5000);
将数据库进程代码放入“file.php”中,只将特定数据从文件返回到ajax。我必须在这一行中重新加载“file.php”的结果:我不知道如何加载。php应该返回“checked”或“”。