Run PHP script in html on button click - php

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

Related

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 form handling

What I'm trying to do is get simple input from the user an print it out using echo within my php tags but not working for some reason. I'm still a beginner to php so if anyone could assist me, that be dope.
Here's my code :
<p>
<form method="GET">
<input type="text" name="firstname">
<input type="submit" value="sub">
</form>
</p>
<?php
echo $_GET['firstname'];
?>
The file is saved with a .php extension and at the moment, I'm running it locally on my computer using apache. The HTML for the form appears but when I click the submit button, it does not echo what was inputted. I know its not going to the code within the php tag but I'm unsure on how to make it go there.
To finish this you will need a action which means this:
<p>
<form method="GET" action="index.php">
<input type="text" name="firstname">
<input type="submit" name="submit" value="sub">
</form>
</p>
Also if you would like to get the result from the form whenever you hit that submit button, you will have to "tell" php that you would like whenever you hit that submit button to print the result. The simpliest way is this:
<?php
if(isset($_GET['submit']))
{
echo $_GET['firstname'];
}
?>
There are two things that you might wanna do:
In the form action write <?php echo $_SERVER['PHP_SELF']; ?>
The PHP_SELF redirect you to the same .php file with the form data
To avoid any exception, use isset function in the last part of your code, which would check if the POST variable exist or not.
<?php
if(isset($_GET['firstname'])){
echo $_GET['firstname'];
}
?>

I can't get this piece of code to work -> $_SERVER['PHP_SELF'] <-

I'm trying to run a very simple script to test the "action=echo $_SERVER['PHP_SELF'];" in a HTML form (with php start and end on it but this site doesn't allow me to introduce those chars). This is the script:
<?php if(isset($_POST['submit'])) { echo "it works"; } ?>
<form name="test" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="submit" value="Submit Form">
Problem is whenever I hit the submit button I get a "Safari can't find the file" error stating that "no file exists at the address" where I run the script.
What am I doing wrong here?
Many thanks in advance for your help!
Eduardo
Try using $_SERVER['REQUEST_URI'] instead of $_SERVER['PHP_SELF']. I think you are using mod_rewrite and that's why it cannot find the file.
You don't need to set the action attribute of the form, if you want to post data to the same page.
<form name="test" method="post">
...
<input type="submit" name="submit" value="Submit Form" />
</form>
should do it.
Cheers!

exec a php to execute shell script

I have a php script like this.
This doesn't run my script nor print anything on the click of the button. How to do that
Also if you have a pointer to php 101 online please let me know
<?php
if ($_GET['run']) {
# This code will run if ?run=true is set.
ob_start();
passthru("./check_ddts.sh ".escapeshellarg($_POST["CSCui21515"]));
$data = ob_get_clean();
}
?>
<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
<button type="button" onclick="?run=true">Click Me!</button>
Your button is not actually doing anything. The problem is that your onclick attribute should contain javascript which will run when the button is clicked, not a redirect location. Use a form with a submit button instead like this.
<form action="" method="get">
<input type="submit" name="run" value="Click Me!" />
</form>
I notice in your question that you are also using post data in handling the form, in which case the above will not work. You need to submit the POST data at the same time as you run the script.

html button to call php shell_exec command

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 &');

Categories