繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM