html button to call php shell_exec command - php

I have google the heck out of this an I cannot get an answer to this. I hate php, but out php guy is too busy and I need HELP!
I want to call a perl script from an html button. But, I just want it to run in the back ground, I don't need to display anything from it... Would something like this work?
<html>
<body>
<p>
<button onclick=<?php exec('test.pl') ?>Run Perl</button>
</p>
</body>
I would prefer not to use cgi, I want to keep this as simple as possible.
Thanks

That will not works, you have to create an action for that:
<?php
if (isset($_POST['button']))
{
exec('test.pl');
}
?>
<html>
<body>
<form method="post">
<p>
<button name="button">Run Perl</button>
</p>
</form>
</body>

Looks like you are trying to call PHP with a JavaScript action. This will not work. You can try submitting a form and executing the PHP code when the form is submitted, like:
<?php if (isset($_POST['button'])) { exec('test.pl'); } ?>
<form action="" method="post">
<button type="submit" name="button">Run Perl</button>
</form>

Addressing the 'run in background' part of this problem, you should be able to put an & at the end to force it to the background.
So exec('test.pl &');

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

Run PHP script in html on button click

I want to run a php script on a button click in html. I've looked at a lot of the posts here, but they don't seem to work. As of now, I'm trying the solution here, but it doesn't work. I have no idea what's causing the trouble, but I know that it isn't the php file. Any help is appreciated.
php script:
<?php
shell_exec("/var/www/html/Camera/CameraScript.sh");
header('Location: /Camera/Camera.html?success=true');
?>
html code:
<form action="Script.php">
<input type="submit" value="Open Script">
</form>
Shell Script:
#!/bin/bash
raspistill --output /var/www/html/Camera/Photos/Image.jpg
If you have use html form like this...
<form action="script.php" method="post">
<input type="submit" value="Open Script" name="btn">
</form>
it's must require put script.php php file in same directory...
<?php
echo "Button Click";
shell_exec("/var/www/html/Camera/CameraScript.sh");
header('Location: /Camera/Camera.html?success=true');
?>
After click on button. if display Button Click then your php file is call and problems is in your php script...
Try this:
<form action="script.php">
<input type="submit" value="Open Script" name="btn">
</form>
script.php
if(isset($_REQUEST['btn']))
{
echo 'Button is clicked';
// you can your shell script here like:
// shell_exec("/var/www/html/Camera/CameraScript.sh");
}
When button is clicked, then the above message is display

Populating a div with php file that processes a form

I have a link on a website when it's clicked it will load a form in a div. The form is processed by a PHP script. I need the output from the PHP script to appear in that div. Any idea how to do this? Thanks
File header.php
<html>
<head>
<script>
$(document).ready(function(){
$('#mylink').click(function(){
$('#content').load("form.php");
});
});
</script>
</head>
<body>
<li>Add Account</li>
file index.php
<?php
include_once 'header.php';
?>
<div id="content"></div>
in form.php
<form id= method="POST" action="script.php">
......
........
<input type="submit" value="Add account" />
</form>
in script.php
i need to send error message or thank you note in div #content
Ajax may help you. It's easy to do so. Try it.
First, this sounds like a job that JavaScript would be perfect for. But if you want to go old school, once you submit your form, depending if you set it to be method="post" or method="get" in the form tag, you can access it with php in the receiving script with $_GET or $_POST.
example:
html:
<form action="yourScript.php" method="post">
<input type="text" name="location" />
php (file: yourScript.php): echo $_POST['location'];

Calling external program with many arguments from website

I am a very new to web programming and perhaps this questions might seem to obvious. I have a form on the website and a button. When the button is pressed I want to call external program (Linux executable) which is located on the server, and pass all the text data from the filled form on the page to that program as arguments, and then get the output back to user. For ex: (./myprogram username userjob ...).
How can this be implemented? What language should I use? Javascript, PHP, Python?
Thank you
You can execute the program with the system call and add the arguments posted by the user to the end of it. It would look something like this:
$theResults = system(escapeshellcmd('./myProgram '.$_REQUEST['arguments']));
echo $theResult;
A full working example would be something like this:
<?php
if(!empty($_REQUEST['arguments'])){
$results = system(escapeshellcmd('./myProgram '.$_REQUEST['arguments']));
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
if(!empty($results)){
echo $results;
}
?>
<form method="post" action="">
Your Arguments: <input type="text" name="arguments" value="" /><input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
You can simply use php for that:
<?php exec("./yourscript.sh");

Run a batch file using php

Below is my piece of code , on giving the tool name as the input and pressing submit , the batch file corresponding to that tool shall be executed.
<html>
<head>
<title>My Form</title>
</head>
<body>
<form action="batch.php" method=post>
Which tool you would like to use:
<br> <input type="text" name="ToolName">
<p>
<input type="submit" name="submit" value="Please wait!">
</form>
</body>
</html>
BATCH.php
<html>
<head>
<title>Perv!</title>
</head>
<?php
$ToolName = $_REQUEST['ToolName'] ;
?>
<p>
Hi <?php print $ToolName;
//exec("cmd/c D:\workspace\execute.bat");
exec("C:\\wamp\\www\\test.bat");
//system("test.bat");
//system("cmd /c D:\\workspace\\execute.bat");
?>
</body>
</html>
I am using Apache /Windows.
Please suggest any help will be appreciated.
As I already commented, what you describe seems to be a problem of your batch file. But anyway, is this file supposed to just do something or to output stuff that should be displayed?
If the later is the case, note that exec() only returns the last line of the output. You can get all the output be providing another variable to get all the output. The official php documentation of the exec() function tells you have to do this.
as far as i could understand your question, you can try this:
system($ToolName);
You may want to specify correct path for the $ToolName variable.

Categories