So I have 2 problems with my code: 1. sending information to the SQL Database With my current code, when I press the toggle switch and the switch slides, I can send updates to the SQL Database "BUT" the problem is there are 3 toggle switches in my website and when I tried to press two or more switches at the same time, some updates may not get sent to the database and my information won't get updated. 2. remaining the checked toggle switches after refreshed page As you see, I use PHP to control the HTML website, so I put header('Location: url') in order to redirect to the HTML page and I want the toggle switch to remain checked if the user has checked it.
The toggle switch I'm using is the one similar to iPhone toggle element.
Settings.html (the toggle switches):
<form method="POST" action="settings.php">
<label class="switch">
<input type="checkbox" name="Streaming" value = "ON">
<span class="slider round"></span>
</label></div></br></td>
<label class="switch">
<input type="checkbox" name="AutoPlay" value = "ON">
<span class="slider round"></span>
</label></div></br></td>
<label class="switch">
<input type="checkbox" name="Notification" value ="ON">
<span class="slider round"></span>
</label></div></td>
<div align = "center"><font face="comic sans ms">
<input type="submit" button class="button" value="SUBMIT" name="submit">
</font></div>
</form>
The php code:
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "XXX";
$conn = new mysqli($servername, $username, $password, $dbname);
$select = "SELECT * FROM `Settings` WHERE ID = 1";
$result = $conn->query($select);
$row = mysqli_fetch_assoc($result);
$streaming_check = $row['Status'];
$select = "SELECT * FROM `Settings` WHERE ID = 2";
$result = $conn->query($select);
$row = mysqli_fetch_assoc($result);
$autoplay_check = $row['Status'];
$select = "SELECT * FROM `Settings` WHERE ID = 3";
$result = $conn->query($select);
$row = mysqli_fetch_assoc($result);
$notification_check = $row['Status'];
if (isset($_POST['submit'])){
if((isset($_POST['Streaming']) == "ON") && $streaming_check == "OFF"){
$sql = "UPDATE `Settings` SET `Status` = 'ON' WHERE `Settings`.`ID` = 1;";
$conn->query($sql);
}
else {
$sql = "UPDATE `Settings` SET `Status` = 'OFF' WHERE `Settings`.`ID` = 1;";
$conn->query($sql);
}
if((isset($_POST['Autoplay']) == "ON") && $autoplay_check == "OFF"){
$sql = "UPDATE `Settings` SET `Status` = 'ON' WHERE `Settings`.`ID` = 2;";
$conn->query($sql);
}
else {
$sql = "UPDATE `Settings` SET `Status` = 'OFF' WHERE `Settings`.`ID` = 2;";
$conn->query($sql);
}
if((isset($_POST['Notification']) == "ON") && $notification_check == "OFF"){
$sql = "UPDATE `Settings` SET `Status` = 'ON' WHERE `Settings`.`ID` = 3;";
$conn->query($sql);
}
else {
$sql = "UPDATE `Settings` SET `Status` = 'OFF' WHERE `Settings`.`ID` = 3;";
$conn->query($sql);
}
}
header('Location: settings.html');
exit;
?>
I think the php code is still wrong in some point: when I toggle 2 switches at the same time, only one toggle switch will turn 'ON'. Or sometimes none of them turn 'ON'.
What I wanted is, if they slide the toggle switch to 'ON' position, the status for that toggle switch will be updated as "ON". Also, the toggle switch on the website will remain checked. If the user slides the toggle switch to turn 'OFF', the status will be updated as "OFF" and the toggle switch on the website will remain unchecked.