I am a hobbyist with limited knowledge of html, javascript, php. I set up a Raspberry Pi microcomputer as a Web server. As a starting point I want to turn on a LED that is connected to the Pi with a button on a Web page. I have the following code that works (name of html file is min.php):
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>LED Control</title>
</head>
<body>
LED Control:
<form method="get" action="min.php">
<input type="submit" value="ON" name="on">
</form>
<?php
$setmode7 = system("gpio mode 7 out");
if(isset($_GET['on'])){
$gpio_on = system("gpio write 7 1");
echo "LED is on";
}
else {
echo "LED is off";
}
?>
</body>
</html>
Now I want to rewrite the code with an ajax function so that the page does not reload when the button is clicked and here I have problems. I looked at a lot of the posted examples but I just can't get over the hump. I changed the html code as follows:
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>LED Control</title>
</head>
<body>
<button type="button" onclick="LED_On()">LED On</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"><?script>
<script>
function LED_On(){
$.ajax({
url:"LED_On.php",
type:"GET",
data:"on"
});
}
</script>
</body>
</html>
The LED_On.php file has the following code and is stored in the same directory as the html file:
<?php
if(isset($_GET['on'])){
$setmode7 = system("gpio mode 7 out");
$gpio_on = system("gpio write 7 1");
echo "LED is on";
}
else {
echo "LED is off";
}
?>
Clicking the button does not turn on the LED. Any help is appreciated.
system()calls did anything?data:{on:"true"}<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"><?script>needs to be<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>