[英]how can I pass the value of html/php page to Python script
I have a very simple question ... I have a two input text field and a output text field in php page (index.php) and a multiply function code in Python (add.py). 我有一个非常简单的问题...我在php页面(index.php)中有两个输入文本字段和一个输出文本字段,在Python(add.py)中有一个乘法函数代码。 Now I want to take the input from php page and calulate the result within python funtion and print the result on php output text field.
现在,我想从php页面获取输入,并在python函数内计算结果并将结果打印在php输出文本字段中。 I do not have any idea how to do it.
我不知道该怎么做。
index.php 的index.php
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Input 1: <input type="text" name="number" value="<?php echo $input1;?>">
<span class="error">* <?php echo $inputErr;?></span>
<br>
<br>
Input 2: <input type="text" name="number" value="<?php echo $email;?>">
<span class="error">* <?php echo $inputErr;?></span>
<br>
<br>
Output:
<textarea name="output" rows="5" cols="40">
<?php echo $output;?>
</textarea>
<br>
<br>
<input type="submit" name="submit" value="Submit">
</form>
multiply.py multiply.py
number1 = int(input("Enter first number: "))
number2 = int(input("Enter second number: "))
mul = number1 * number2
print("Multiplication of given two numbers is: ", mul)
First you'll have to change your python script so it can gets numbers from the commandline arguments instead of reading from stdin. 首先,您必须更改您的python脚本,以便它可以从命令行参数获取数字,而不是从stdin读取。 You may also want to change the output so it only prints the result without other unrelated text:
您可能还想更改输出,以便仅输出结果而没有其他不相关的文本:
# add.py
# NB : this should really be named 'multiply', not 'add'
import sys
try:
number1 = int(sys.argv[1])
number2 = int(sys.argv[2])
except (IndexError, ValueError):
sys.exit("usage: python add.py <number1> <number2>")
result = number1 * number2
print(result)
Then on the php side it's just a matter of calling the script with exec
with the proper command string (you have to insert the numbers in the command string) and reading the result from the $ouput
array. 然后在php端,只需使用带有正确命令字符串的
exec
调用脚本 (您必须在命令字符串中插入数字)并从$ouput
数组读取结果即可。
This being said, one can only wonder why you want to call a Python script to do such a simple thing as a multiplication... 话虽这么说,但人们只能想知道为什么要调用Python脚本来完成诸如乘法之类的简单操作...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.