我是一个PHP新手,正在做一个用户界面。这是一个个人断路器项目,其中瓦数和断路器状态(ON/OFF)被发送到数据库。
我现在尝试的是为界面制作一个实时状态按钮。本质上,我希望按钮类/背景颜色根据数据库中的状态行改变。(Status = 0 ->红色状态按钮,Status = 1 ->绿色状态按钮)。
我需要帮助的是数据库的状态值的自动查询每2-3秒。我知道如何查询我想要使用mysql/php读取的值,以及如何使用Javascript函数切换按钮类/背景颜色。
我应该使用AJAX的自动化过程?
这是我正在玩的模块:JSFIDDLE
这是最新的来源:
<!DOCTYPE html>
<html>
<head>
<script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(this).toggleClass("off");
});
});
</script>
<style>
div{
float: left;
color:#fff;
font-size:40px;
}
h2{
text-align: center;
}
.wrapper{
position: absolute;
top: 25%;
left: 10%;
margin-right: -50%;
transform: translate(-50%, -50%);
}
.one{
width: 125px;
height:100px;
}
.two{
width: 120px;
height:75px;
background:darkblue;
}
.three{
width:120px;
height:75px;
background:blue;
}
.button{
background-color: #4CAF50; /* Green */
border: 1px solid green;
color: white;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
float: left;
position: relative;
left: 15%;
}
.button2{
background-color: #4CAF50; /* Green */
border: 1px solid green;
color: white;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
float: left;
position: relative;
left: 15%;
}
.off {
background: red;
}
.button:hover {
background-color: #3e8e41;
}
.button2:hover {
background-color: #3e8e41;
}
</head>
</style>
<?php
$query ="SELECT status FROM ICBP.radio3 ORDER BY time DESC LIMIT 1;";
$result = mysql_query($query);
$status = mysql_fetch_row($result);
if($status = 0){
echo "off";
}
else if ($status = 1) {
echo "on";
}
?>
<body>
<h2>ICBP Dashboard</h2>
<div class="wrapper">
<div class="one">
<div class="two">
<a href="index3.php"><button class="button">Breaker 1</button></a>
<button class="button2">Status</button>
</div>
<div class="three">
<a href="index3.php"><button class="button">Breaker 2</button></a>
<button class="button2">Status</button>
</div>
</div>
<div class="one">
<div class="two">
<a href="index3.php"><button class="button">Breaker 3</button></a>
<button class="<?php echo $status ?>">Status</button>
</div>
<div class="three">
<a href="index3.php"><button class="button">Breaker 4</button></a>
</div>
</div>
</div>
</body>
</html>
任何关于我的方法的指导都是非常感谢的。
谢谢!
是的,我会向PHP脚本发出AJAX请求,该脚本查询数据库并返回当前状态(内容类似于PHP标记之间的内容)。
请求需要从setInterval函数触发:https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval
编辑
展开答案…我会将代码分成单独的脚本,如下所示:
HTML<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body>
<button class="btn-grey"> </button>
<!-- loading jQuery here as I'm using it within script.js -->
<script
src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
styles
css.btn-green {
background-color: green;
}
.btn-red {
background-color: red;
}
.btn-grey {
background-color: grey;
}
status.php
<?php
// perform the query
// echo out the status depending on the result of the query:
echo 1;
script.js(此处修改为您需要的代码版本:https://stackoverflow.com/a/5052661/1075071)
// this function is going to be called on page load and then in 5s intervals
(function worker() {
// fire AJAX request to status.php
$.ajax({
url: 'status.php',
success: function (data) {
// process the data received from the PHP script
if (data === '1') {
$('button').removeClass();
$('button').addClass('btn-green');
} else if (data === '0') {
$('button').removeClass();
$('button').addClass('btn-red');
} else {
$('button').removeClass();
$('button').addClass('btn-grey');
}
},
error: function () {
$('button').removeClass();
$('button').addClass('btn-grey');
},
complete: function () {
// Schedule the next request when the current one's complete
setTimeout(worker, 5000);
}
});
})();