doulu1020 2018-06-17 09:55
浏览 1423

如何从HTML调用php函数

I am playing around with GPIO pins on a raspberry pi and have been trying to teach myself how to get them to function via a webpage, I am familiar with python, CSS and HTML but not so much with javascript so I am struggling with this code.

I followed a tutorial on youtube where I built a basic webpage with 2 buttons that turns on and off an LED on a breadboard.

As I have the code written there, the code works fine, the LED's turn on and off.

I am trying to add more buttons to the page but I am having trouble accessing the javascript so the LED will turn on. The functioning web page is as follows.

   <!DOCTYPE html>
<html>
        <head>
        <style>
        </style>
                <meta charset="UTF-8">
                <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
                <script type="text/javascript">
                        $(document).ready(function(){
                                $('#clickOn').click(function(){
                                        var a = new XMLHttpRequest();
                                        a.open("GET","pinOn.php");
                                        a.onreadystatechange=function(){
                                                if(a.readyState==4){
                                                        if(a.status == 200){
                                                        }
                                                        else alert("HTTP ERROR!");
                                                }
                                        }
                                        a.send();
                                });
                         $('#clickOff').click(function(){
                                        var a = new XMLHttpRequest();
                                        a.open("GET","pinOff.php");
                                        a.onreadystatechange=function(){
                                                if(a.readyState==4){
                                                        if(a.status == 200){
                                                        }
                                                        else alert("HTTP ERROR!");
                                                }
                                        }
                                        a.send();
                                });
                        });
                </script>
                <title>Pi GPIO Controller</title>
        </head>
        <body>
                <h1><center>Turn the light on and off!</center></h1></br></br>
                <div class="button">
                <center><button type="button" id="clickOn">ON</center></button></br>
                <center><button type="button" id="clickOff">OFF</center></button></br>
                </div>
        </body>
</html>

This is the pinOff.php file that sets pin 4 to high.

<?php
        system("gpio -g mode 4 out");
        system("gpio -g write 4 1");
?>

And the pinOn.php to set it to pin low.

<?php
        system("gpio -g mode 4 out");
        system("gpio -g write 4 0");
?>

I am trying to add another button and have tried simply adding:

<center><button type="button" id="clickOff">OFF</center></button></br>

But it isn't working.

How can I call the javascript in a button to get the code in the pinOn and pinOff files to work.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?