Passing variable from PHP button to python script
Get the solution ↓↓↓I've create PHP script that takes in user input and sends it to a Python script. The Python script creates an image which the PHP script displays.
Here's my Python code:
import sys
import matplotlib.pyplot as plt
import numpy as np
import ftplib
result = sys.argv[1]
x = np.arange(0, result, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.savefig('image.png')
My PHP code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Contact Form</title>
</head>
<body>
<h2>Value identifier</h2>
<p>Please fill in the value:</p>
<form action="" method="post">
<p>
<label for="inputName">Name:</label>
<input type="text" name="value" id="inputName">
</p>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$values=$_POST['value'];
$rad=exec("python test.py".$values);
echo $values;
echo $rad;
echo "<img src='image.png'>";
?>
I don't get anything published, as if the Python script isn't even running. But printing the values I want to pass is successful.
Answer
Solution:
The key line:
$rad=exec("python test.py".$values);
``
shouldn't be concatenated. The period should be a comma. Also, you don't need python before test.py. You do need the filepath, however. Also, you may want to try shell_exec() instead of exec() because that will just return a string, and you can print out the string to see what you are getting. See below:
$rad = shell_exec('/home/path/path/path/RELATE/main.py')
Separately, this question is similar to the one you are asking and gives you a separate way to get PhP to talk to Python using a text file as an intermediary. The benefit of this is that it would at least help you identify whether you have a PhP problem or a Python problem when there is a bug in your code.
https://stackoverflow.com/q/47981370/9807545
Answer
Solution:
You are concatenating your arguments together without a space between them. Further, you must always escape values before passing them to the shell. Use .
<?php
if (isset($_POST["value"])) {
$value = $_POST["value"];
$rad = exec("python test.py ". escapeshellargs($value));
echo $value;
echo $rad;
echo "<img src='image.png'>";
}
?>
You will also need to ensure that the user running the web server process has write permissions to the directory the scripts are contained in. This is unlikely to be the case, and certainly should not be the case. I'd suggest setting up a separate directory with appropriate permissions.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: php undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.