gpt4 book ai didi

javascript - 在 PHP 上更新 MySQL 变量而不刷新

转载 作者:行者123 更新时间:2023-11-29 11:48:57 25 4
gpt4 key购买 nike

我正在从用 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()
});

关于javascript - 在 PHP 上更新 MySQL 变量而不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34483241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com