Executing a .sh file with php - php

I have written a very simple Hello World script as a bash file and saved it on my desktop.
I want to execute this file when a Submit button is pressed.
The following script works fine and I see the Hello World on the firefox webbrowser:
<?php
echo shell_exec('sh /home/administrator/Desktop/Helloworld.sh');
?>
However the following script does not give me any result:
<!DOCTYPE html>
<html>
<body>
<?php
if (isset($_POST['Submit1'])) {
echo shell_exec('sh /home/administrator/Desktop/Helloworld.sh');
}
?>
<Input Type = "Submit" Name ="Submit1" Value = "Save Parameters">
</body>
</html>
Any idea why? I don't get any errors in my log file.

You are not posting anything, for that you'll need a form, i.e.:
file.php
<html>
<body>
<?php
if (isset($_POST['Submit1'])) {
echo shell_exec('sh /home/administrator/Desktop/Helloworld.sh');
}
?>
<form action="file.php" method="post">
<input Name= "Submit1" type="submit">
</form>
</body>
</html>
Also, make sure the file /home/administrator/Desktop/Helloworld.sh is executable, i.e.:
chmod +x /home/administrator/Desktop/Helloworld.sh

Related

Get variable from Post Data (PHP)

I really don't know why my code isn't working! Let me clear it by example..
I got a file named 'index.html' example...
<html>
<body>
<form action="test.php" method="post">
Name: <input type="text" name="test">
<input type="submit">
</form>
</body>
</html>
And of course the form action file test.php .. example..
<?php
$something = ($_POST["test"]);
// from here I have some PHP codes and this "something" variable will be process in this codes and will print out something spacial..
?>
Now example If I post "Hello, It's not working" then the output will show a spacial design.
But Instead process, it's just printing out whatever I submit in that form.
But when I manually add the variable to "something" and if I execute the "test.php" . Example..
$something = "Hello, It's not working";
Then it works perfectly..
And yes. Also tried GET method.. It's still same as POST.
This is my first question here..
Thanks for any help and suggestions!
First Convert, index.html to php and follow this code:-
<html>
<body>
<form action="test.php" method="post">
Name: <input type="text" value="" name="test">
<input name="submit" value="submit" type="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
$something = $_POST["test"];
}
?>
That's correct _POST:
<?php
//NO ($_POST["test"])
$something = $_POST["test"];
?>

PHP execute shell script and provide inputs for prompts

I have an apache server on a raspberry pi. I have a basic program where the user inputs a string into an html form box. Clicking submit sends the entry to a php file, which then calls a shell script, and passes the entry as a prompt. If you look at the php below, it has a variable with the user input, and it calls the shell file main.sh. How can I provide the shell program that variable as an input. I have looked at proc_open, but haven't been able to get it to work. Thanks
Index.html:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="script.php" method="post" enctype="multipart/form-data">
Address: <input type="text" name="Address" value=""><br>
<input type="submit" value="Upload" name="submit">
</form>
</body>
</html>
script.php:
<?php
if(isset($_POST["submit"])) {
if($_POST['Address'] != "")
{
$entry = $_POST['Address'];
$output = shell_exec ("./main.sh");
echo $output;
}
}
?>
and finally the shell file: main.sh:
echo -n "Do you like pie (y/n)? "
read answer
if echo "$answer" | grep -iq "^y" ;then
echo Yes
else
echo No
fi
you can pass variable to shell script as follows
$output = shell_exec("./main.sh $entry");
and in shell script , Add x = $1 . $x would be variable, with your user input value.
Look for this post for further details https://stackoverflow.com/a/19460686/3086531

How to get a PHP variable value from another PHP file?

how to take the value from another .php file help me fix this:
<html>
<head>
</head>
<body>
<form name='answer' action="file2.php" method="post">
<input type="text" name="what">
<input type="submit" value="Login">
</form>
<div><?php echo $answer; ?></div>
</body>
</html>
And the code of file2.php file is as below:
<?php
if ($_POST['what']==animal){
$answer="dog";
}else{
$answer= "not animal";
}
I would like to know how can I get the value of the variable $answer with button, then what should I put at the line :
Just use requiere
<?php require 'file2.php'; ?>
Use require_once() to include file2.php in your php file, then in your file you can access the variables in file2.php.
require_once("path of file2.php");
echo $answer;

How to get value to work in php and html tag?

The first file (which is html) has two inputs and the second file (php) is the form to display the data when submitted. I want the php file to print whatever message is typed into the input and submitted.
html file:
<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
welcome_get.php file:
<html>
<body>
Welcome <?php echo $_GET["name"];?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
I know it is probably very simple but for some reason, whenever I type something into the input and submit, it doesn't display the values I inputted. It just displays only the message in the php file:
Welcome
Your email address is:
Use isset to check the variables exist before you use them. i.e.
<html>
<body>
<?php
if(isset($_GET['name'])){
echo 'Welcome '. $_GET["name"] .'<br>';
}
if(isset($_GET['email'])){
echo 'Your email address is: '. $_GET["email"] .'<br>';
}
?>
</body>
</html>
additionally, it's worth ensuring you have all errors displayed by putting this at the top of your file:
<?php
display_errors(E_ALL);
ini_set('display_errors', 'on');
?>
finally, check you're php install is working correctly, by creating a new file (phpinfo.php) with the following contents:
<?php
phpinfo();
?>
open this in the browser, and check you get the about php page.

PHP - Can't submit form to handler

Here is my Code :
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" action="../../../wamp/www/abc.php" method="get">
<input type="text" name="text1" id="username" />
<input type="submit" />
</form>
</body>
</html>
The abc.php file is located in C:/wamp/www/abc.php
<html>
<body>
Hello World
<?php
echo "Hello";
echo $_GET["username"];
?>
</body>
</html>
In the form, when I press the Submit button, only "Hello World" is displayed.
The value entered in the textbox is not displayed at all. Even the "Hello" message printed inside the php code is not displayed.
How can i display the Value ?
text1 is the name of your input, so the correct PHP code would be.
<?php
echo "Hello ";
echo $_GET['text1'];
?>
Try the below, get array is created using name attr not id one.
echo $_GET["text1"];
There is two solution for you should use :
First Way :Change your php code:
<?php
echo "hello";
echo $_GET["text1"];
?>
Second Way: Chnage your HTML
<input type="text" name="username" id="username"/>
If second "Hello" from echo is not displayed, there is a problem with your PHP installation.

Categories