使用PHP将单个变量从HTML传递到Python脚本

时间:2018-04-12 02:38:46

标签: php python html apache

我正在尝试设置一个系统,当一个数字输入到网页时,它会触发一个计时器并打开一个 GPIO 引脚 RPI 几秒钟然后关闭。

我需要数字提交到 HTML表单中的数字添加到我的 python脚本中与...合作。

我以为我找到了一个解决方案,但我不确定为什么它不起作用。我正在使用 PHP7 Pi零 W上运行apache2。

以下是我在 / var / www / html / 目录中的两个文件:

的index.php:

<form action="index.php" method="post">
    <input type="text" name="seconds"/>
    <input type="submit" name="SubmitSeconds" /><br/>
</form>

<?php
if(isset($_POST['SubmitSeconds'])) {
    shell_exec('/usr/bin/python /var/www/html/script.py'.$seconds);
}
?>

script.py:

#!/usr/bin/python

from datetime import datetime
from threading import Timer
import RPi.GPIO as GPIO
import time, cgi, sys

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)

def GPIOTimer():
    GPIO.output(23, 1)
    time.sleep(10)
    GPIO.output(23, 0)

def ten_seconds():
    global x
    global y
    x = datetime.today()
    y = x.replace(day=(x.day), hour=(x.hour), minute=(x.minute), second=(x.second+10), microsecond=0)

def five_mins():
    global x
    global y
    x = datetime.today()
    y = x.replace(day=(x.day), hour=(x.hour), minute=(x.minute+5), second=(x.second), microsecond=0)

def ten_mins():
    global x
    global y
    x = datetime.today()
    y = x.replace(day=(x.day), hour=(x.hour), minute=(x.minute+10), second=(x.second), microsecond=0)

def thirty_mins():
    global x
    global y
    x = datetime.today()
    y = x.replace(day=(x.day), hour=(x.hour), minute=(x.minute+30), second=(x.second), microsecond=0)

def one_hour():
    global x
    global y
    x = datetime.today()
    y = x.replace(day=(x.day), hour=(x.hour+1), minute=(x.minute), second=(x.second), microsecond=0)

def three_hours():
    global x
    global y
    x = datetime.today()
    y = x.replace(day=(x.day), hour=(x.hour+3), minute=(x.minute), second=(x.second), microsecond=0)

def TimerMain():
    global x
    global y
    global delta_t
    global secs
    global t
    delta_t=y-x
    secs = delta_t.seconds+1
    t = Timer(secs, GPIOTimer)
    t.start()

def TimerSequence(z):
    if (z==1):
        ten_seconds() 
    elif (z==2):
        five_mins
    elif (z==3):
        ten_mins()
    elif (z==4):
        thirty_mins()
    elif (z==5):
        one_hour()
    elif (z==6):
        three_hours()
    else:
        print("InputError")
    TimerMain()

result = int(sys.argv[1])
TimerSequence(result)

1 个答案:

答案 0 :(得分:1)

1)您忘记声明变量“seconds”

<form action="index.php" method="post">
    <input type="text" name="seconds"/>
    <input type="submit" name="SubmitSeconds" /><br/>
</form>

<?php
if(isset($_POST['SubmitSeconds'])) {
    $seconds = $_POST["seconds"];
    shell_exec('/usr/bin/python /var/www/html/script.py'.$seconds);
}
?>