Running Python scripts from HTML button

2020-04-18 08:39发布

问题:

P am trying to implement a button that will run a Python script forward.py (move my robot forward) using Lighttpd web server/PHP 7.0.

The Python script works perfectly when executed in the terminal window however when I try to run it from the click of the button it does not work.

Here is my basic HTML code to execute the .py script:

<html><body>
<h1> Development of Internet controlled robot </h1>
<input type="button" name="forwardbutton" value=" Move forward "onClick="<? exec(' python /var/www/html/forward.py'); ?>">
</body></html>

I have changed the owner of the forward.py script to be www-data however this has made no difference.

I am quite new to working with Raspberry Pis.

回答1:

I am not sure you can open php tags inside onClick event. You should call javascript function maybe and use ajax (to avoid refresh) to call the php page which will have the exec(python program) function.

take a look here Execute PHP function with onClick