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