[英]update MySQL variable on PHP without refresh
我正在用PHP編寫的網頁在Raspberry Pi的串行上讀寫字符串。
我需要刷新設備按鈕的顏色而不刷新頁面。 在這里,我的代碼現在可以正常工作,但是只有重新加載頁面后顏色才會改變。
每次單擊按鈕時,我都需要再次詢問數據庫並更改按鈕的顏色。
<?php
//General ( I use this part to send the string to the serial)
if (isset($_POST['GeneralON']))
{ exec('/home/pi/myfolder/myCProgramm @mytringON');
}
if (isset($_POST['GeneralOFF']))
{ exec('/home/pi/myfolder/myCProgramm @mytringOFF');
}
//I connect to mydb and i read the status of device 91
$user_name = "myuser";
$password = "mypassord";
$database = "mydb";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found)
{
// I read the status of device 91
$SQL = "SELECT * FROM devices WHERE id = 91";
$result = mysql_query($SQL);
while ( $db_field = mysql_fetch_assoc($result) ) { $mybedroom = $db_field['state']; }
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}
?>
<body align="center" style="font-size:70">
<form method="post" >
<div class="myrow">
<div class="myleft">
General Lamps
</div>
<div class="myright">
// if the variable my bedroom is 1 I set red the color of button by CSS class
<?php if ($mybedroom == 1): ?>
<button class="btnred" name="GeneralON">ON</button>
<?php else: ?>
// if the variable my bedroom is 0 I set blue the color of button by CSS class
<button class="btn" name="GeneralON">ON</button>
<?php endif ?>
<button class="btn" name="GeneralOFF">OFF</button>
</div>
</div>
</form>
</body>
我知道這段代碼確實是“粗魯的” ...但是現在是我最好的:)
有什么建議嗎?
我正在嘗試使用Ajax在單擊按鈕時刷新變量$ mybedroom,但是我不能這樣做。
謝謝
var color = "<?php if ($mybedroom == 1){ echo 'red'}else{ echo 'green'} ?>";
function btncolor(){
if (color=="red"){
$('.btn').css('color', 'green');
}else{
$('.btn').css('color', 'red');
}
}
$('.btn').click(function(){
// something changes php vars and print the new color
btncolor()
});
抱歉,我對答案不太了解!!!! 我的PHP和腳本知識很低:(
沒用
我的CCS課程是:
普通按鈕:
.btn {
background: #3498db;
background-image: -webkit-linear-gradient(top, #3498db, #2980b9);
background-image: -moz-linear-gradient(top, #3498db, #2980b9);
background-image: -ms-linear-gradient(top, #3498db, #2980b9);
background-image: -o-linear-gradient(top, #3498db, #2980b9);
background-image: linear-gradient(to bottom, #3498db, #2980b9);
-webkit-border-radius: 28;
-moz-border-radius: 28;
border-radius: 28px;
font-family: Arial;
color: #ffffff;
font-size: 60px;
padding: 10px 20px 10px 20px;
margin-left: 20px;
text-decoration: none;
align: right;
}
紅色按鈕:
.btnred {
background: #3498db;
background-image: -webkit-linear-gradient(top, #FF0000 , #B00000 );
background-image: -moz-linear-gradient(top, #FF0000 , #B00000 );
background-image: -ms-linear-gradient(top, #FF0000 , #B00000 );
background-image: -o-linear-gradient(top, #FF0000 , #B00000 );
background-image: linear-gradient(to bottom, #FF0000 , #B00000 );
-webkit-border-radius: 28;
-moz-border-radius: 28;
border-radius: 28px;
font-family: Arial;
color: #ffffff;
font-size: 60px;
padding: 10px 20px 10px 20px;
margin-left: 20px;
text-decoration: none;
align: right;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.