Run python script on a php android server - php

I have a webserver in Android using KSWEB application. It can support php, mysql, etc. I also have installed SL4A and python interpreter on my Android. I want to execute a python script (that runs on server side, the Android) on the click of a button.
All I have for now, is this:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<button name=test>TEST</button>
</body>
<?php
if(isset($_POST['test']))
{
exec('python test_andrei.py');
echo ('Worked!');
}
?>
</html>
Of course nothing happens when I click the button (both on server side (no script executed) and on client side (no echo output))... I am a complete noob in php and python...
Thank you for your help!

First of all wrap the name value in quotes for the button:
<button name="test">TEST</button>
Secondly you need to wrap the button (and any other inputs) in a form for that isset($_POST['test']) to be submitted as expected:
<form method="post" action="<?php echo html_entities($_SERVER['PHP_SELF']); ?>">
<button type="submit" name="test">TEST</button>
</form>
and also as you can see i added the action to the form so it submits to the same page

Related

How to execute python script from php?

I'm new to php and html (internet languages in general) and I would like to execute a python script that takes a picture from my php page.
I did some research, edited some code (according to my understanding) and came up with this, but it doens't do anything.
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="" method="post">
<button type="submit" name="sub" value="call">Click</button>
</form>
</body>
</html>
<?php
if(isset($_POST['sub']))
{
exec('sudo python take_picture.py');
echo "Picture captured";
}
?>
I managed to execute the python script from a html page invoking a simple php script separately, but when it takes the picture it sends me to a new page (blank page with the text displayed) which I don't want. Here are the codes:
HTML button:
<form action="take_picture.php" method="post">
<button>Click</button>
</form>
PHP script:
<?php
exec('sudo python take_picture.py');
?>
What I need is to simply press the "click" button and take the picture without sending me to anywhere else.
Could you please guys aid me to achieve what I need and explain with apples what I was doing wrong or what I missed.
Thank you in advance!
You can make an :
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>
More information on this Stack Overflow Question.
Running a Python script from PHP

How to execute Python scripts from HTML button

My html page has a button and when I click that button I need to execute a python file. I don't need to show the python file content in browser, I only need to execute the python file by clicking the html button. When executing the python file, it will generate a json file which I need.
This is my code and it's opening the python file but not showing any output.
<html>
<body>
<h1> DEVELOPMENT </h1>
<form action="phpfile.php" method="post">
<input type="submit" name="someAction" value="GO" />
</form>
</body>
</html>
<?php
$result_last_line = system("python C:/xampp/htdocs/NEW/pythonfile/identifyrooms.py");
header('Location: view.html');
exit;
?>
When I run my python file using editor the output console is as below.
Can anyone find the error?.
Thanks

How can my PHP code run when i submit a HTML form

I am trying to make a website using python and in flask I have a webpage that has a submit button, when the button is pressed I need it to run a python file I have called Answers.py
from flask import Flask, request, render_template, send_from_directory
app = Flask(__name__)
#app.route('/help', methods=["GET","POST"])
def help():
return render_template("help.html")
the HTML code on the page is this and i am trying to get when the button is pressed it runs a php script that outputs the value from a separate python file, the output text.
<html>
<body>
<form action="" method="post">
<button type="submit" name="sub" value="Hello world">Submit
call</button>
</form>
</body>
</html>
I have tried this php code in the same file as the HTML but nothing is output when I press the button, i'm new to php so i'm not sure what the problem is.
<?php
if(isset($_POST['sub']))
{
$result = exec("Python Path Answers.py /tmp");
echo $result;
}
?>
The php script never gets referenced in the html page!
<form action="" method="post"><!-- action there is empty! -->
You should reference the php script in the action attribute of the <form> tag, like so:
<form action="myscript.php" method="post">

PHP contact form returning the PHP code when I press the submit button

I created a contact form for my website. When I click the submit button, it just shows the PHP code instead of the message it is supposed to show, like the text information the user inputs into the form.
Here is the HTML code for the form:
<html>
<link href="contact.css" rel="stylesheet" type="text/css" />
<head>
<title>Contact Us</title>
</head>
<body>
<form action="contact.php" method="post">
<div id="ContactForm">
<fieldset>
<h4>Contact Parquest:</h4>
<label class="labelone" for="name">Name:</label>
<input name="name"/>
<label for="email">Email: </label>
<input name="email"email"/>
<label for="commens">Comments: </label>
<textarea name="commens"></textarea>
</fieldset>
<fieldset>
<input class="btn" type="submit" value="Send Email"/>
<input class="btn" type="reset" value="Reset Form"/>
</fieldset>
</form>
</div>
</html>
I couldn't post the PHP code, because it just doesn't show when I try, so here's a screenshot:
There is no problem to your code.. The problem is on your environment.. I guess you are not running the html file through a server..
If the url on your browser looks like these:
file:///c:/path/to/your/file/page.html
then you are doing it wrong.. in order to run .php scripts, you need a web server like apache or nginx... the url of should be like these
http://localhost/path/to/file/page.html
then the php file should run as expected..
php files are interpreted scripting language and thus it needs an interpreter in the server in order to run.. if it is just browsed in the browser without a server, it will just output the code inside..
You need a web server to run php scripts.
Try installing wamp and then run your page under localhost.
I have executed the given code. It's working fine on my local server. Conventionally it should print the value inputs posted by form on contact.php.
But the thing I noticed in your PHP code is the variable name is $commets, instead of $comments.
I faced the same problem and when I tried starting my own server it worked.
when you are using Windows, open cmd and type in this;
php -S localhost:4040;
you will see something like this
PHP 8.1.1 Development Server (http://localhost:4040) started
meaning you have started your own server, then you can copy the link found in the parenthesis
so you copy say this "http://localhost:4040" and paste it in your browser then you can now direct it to your html document like this
http://localhost:4040/Desktop/Projects/index.html
now your form should work well with your php script when you submit
i had the same issue, even if appache & mysql were running on my computer.
in the html file, i wrote :
form action="http://localhost/file.php" method="POST"
instead of :
form action="file.php" method="POST"
It seems like connecting HTML forms to php files only works when you download wampserver and turn on all services...
Here's how it worked for me:
You can download wampserver from here: http://www.wampserver.com/en/ (follow instructions)
I saved it to the C: drive on my computer.
Next, you can create a php file and an html file both in the 'www' folder of the wamp folder in whatever drive you put it in. Copy the form code into the HTML file, and the php code into the php file.
Finally, go to 'localhost/yourhtmlname.html' and fill out the form. It should work as normal.

Running .bat script from .php web page

I am attempting to load a webpage on my own server which will run a .bat script (on the same server) as below.
When I access the page, called test.php, it display the 'DO IT!' button and when I press it, it just display the content on the .bat file rather than executing it on the server...
What do I need to configure on the server, I assume in the PHP settings, to force it to run the script rather than just display it on the webpage?
For the purpose of the question, I am happy about the security implications of what I am doing.
I am running a Windows machine with IIS and PHP.
<html>
<head>
<title>Restarting</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
echo exec('c:\scripting.bat');
echo "Done!";
} else {
// display the form
?>
<form action="" method="post">
<input type="submit" name="submit" value="DO IT!">
</form>
<?php
}
?>
</body>
</html>
I think that the echo exec('c:\scripting.bat'); line it's causing you the problem. Try to just execute it without the echo statement.
If you trying to see the output of the function, you must use the second functions parameter: &$output, acording to the documentation itself. See it in the docs here.
I hope it will be useful to you! :D

Categories