Background - I have a RPI with several IR Beams, PIRs and reed switches around the house connected. A Python script monitors the states of pins and announces audibly via a wireless baby monitor if there is a trigger. There is a bank of toggle switches attached to the I/O pins to disable individual zones or change behavior like send sms or email video grabs via the Python script. The Pi is also a LAMP server and the Python script logs events in MySQL. The logs can be viewed via web browser.
My aim is to remove the bank of toggle switches and replace with check boxes in a web form, updating values 0 or 1 in db which then the Python script will use. I have been putting together a single example code to get it working before I expand it to the dozen or so values I want to control.
What I am trying to do with the code below is upon opening the webpage have the checkbox display current db value and if the user checks/unchecks the box and hits submit then the db is updated and the page refreshes to the up to date information. My code doesn't quite behave as I want as it only returns a zero to the database regardless of the state of the checkbox when the "submit" button is activated.
Any help is greatly appreciated.
<!DOCTYPE html>
<html>
<head>
<title> checkbox wip</title>
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<link rel="stylesheet" href="style.css">
</head>
<body>
<?php
$servername = "localhost";
$username = "*****";
$password = "*****";
$dbname = "Monitor";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$thesql = "SELECT event FROM sw_sleep ORDER BY id DESC LIMIT 1";
$result = mysqli_query($conn, $thesql) or die(mysqli_error($conn));
$row_result = mysqli_fetch_assoc($result);
$SSwA = $row_result['event'];
mysqli_close($conn);
?>
<form action="update.php">
Sleep<input type="checkbox" id="sleepstat" name= "sleepstat1" value="1"> <br/>
<script>
document.getElementById("sleepstat").checked = <?php echo $SSwA?>;
</script>
<input type="submit" value="Submit">
</form>
</body>
</html>
And this is update.php
<?php
$servername = "localhost";
$username = "******";
$password = "******";
$dbname = "Monitor";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$val = $_POST['sleepstat1'];
$sql="INSERT INTO sw_sleep (event) VALUES ('$val')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
header("Location: index.php");
Aucun commentaire:
Enregistrer un commentaire