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

时间:2015-12-27 17:59:51

标签: javascript php mysql ajax page-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>

我知道这段代码真的很粗鲁&#34; ...但现在它是我最好的:))

有什么建议吗?

我试图在按钮点击时使用ajax刷新变量$ mybedroom,但我不能这样做。

谢谢

2 个答案:

答案 0 :(得分:1)

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()
});

答案 1 :(得分:0)

抱歉,我不太清楚答案!!!!我的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;
}