Reputation: 1
I'm reading and writing string on serial of my Raspberry Pi from a web page written in PHP.
I need to refresh the color of buttons of my device without refreshing the page. Here my code that now is working but the color change only if a reload the page.
I need to ask again the DB and change the color of the button every time I click the button.
<?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>
I know this code is really "rude"... but now it is my best :)
Any suggestion?
I'm trying to use ajax to refresh the variable $mybedroom on button click, but I cannot do that.
Thank you
Upvotes: 0
Views: 237
Reputation: 1
Sorry, I don't understood very well the answer!!!! MY PHP and script knowledge is very low :(
It doesn't work
My CCS class are:
normal button :
.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;
}
red button :
.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;
}
Upvotes: 0
Reputation: 471
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()
});
Upvotes: 1