This is my first time posting to this site so I hope this is a good question. I've tried my best to google and watch videos but I cant seem to get my code to work as I am a novice with web dev as a whole.
So, basically I made a basic website that is meant to turn my lights on and off via an apache server on my Raspberry PI, however I cant seem to get the php code to execute when I use a slider/checkbox but when I use a normal button for 'on' and 'off' respectively the php code runs fine.
</head>
<body>
<center><h1>on or off... yes this is all it does</h1>
<form method="get" action="index.php">
<input type="submit" style = "font-size: 14 pt" value="OFF" name="off">
<input type="submit" style = "font-size: 14 pt" value="ON" name="on">
</form>
</centre>
<?php
shell_exec("/usr/local/bin/gpio -g mode 17 out");
if(isset($_GET['off']))
{
echo "Light is off";
shell_exec("/usr/local/bin/gpio -g write 17 0");
}
else if(isset($_GET['on']))
{
echo "Light is on";
shell_exec("/usr/local/bin/gpio -g write 17 1");
}
?>
This code runs fine and I can turn the lights on and off with no problems, however...
<div class="column">
<div class="main-switch">
<h1>Main Terminal</h1>
<form action="index.php" method="post">
<label class="switch">
<input type="checkbox" name="checkbox_name" id="switch1" value="OFF" onclick=lights()></input>
<span class="slider"></span>
</label>
</form>
<?php
shell_exec("/usr/local/bin/gpio -g mode 17 out");
if(isset($_POST['checkbox_name'])&& $_POST['checkbox_name']=='OFF')
{
echo "Light is off";
shell_exec("/usr/local/bin/gpio -g write 17 0");
}
else
{
echo "Light is on";
shell_exec("/usr/local/bin/gpio -g write 17 1");
}
?>
This code does not seem to work at all.
It will always seem to default to the else statement regardless of the value of the checkbox. The onclick function I call is to change the checkbox value to ON and OFF respectively based on it's position and when I print it in the console these values are changing. I'm pretty sure it's the garbage php I wrote as I am a complete novice with websites as a whole.
As you can see I also tried a few things in the second portion of the code, I tried changing the method to post and using some other code I found online but it didn't work. I tried a lot other things like ajax query and other variations of the code above but they also did the same thing and didn't run.
I am aware it is my lackluster knowledge on php but after a lot of googling and videos I couldn't understand why it wasn't working. So if anybody could just help me in the right direction I'd appreciate it.
edit:Javascript code
function lights(){
var toggle = document.getElementById('switch1');
if(toggle.checked){
toggle.value="ON";
console.log(toggle.value);
}else{
toggle.value="OFF";
console.log(toggle.value);
}
}