简体   繁体   English

如何将html / php页面的值传递给Python脚本

[英]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.

相关问题 如何将javascript的值传递给html表单上的php? - How can I pass the value from a javascript to php on an html form? 如何将 HTML 值传递给 PHP 变量 - How can I pass an HTML value to a PHP variable 我无法将一些值从html复选框数组传递到我的php页面? - I can't pass some value from html checkbox array to my php page? 将变量从HTML页面传递到PHP脚本 - Pass Variable From HTML Page To PHP Script 如何将HTML文件内容从PHP传递到CGI(Perl)脚本 - How can I pass an HTML file content from PHP to CGI (Perl) Script 如何通过POST将值传递给php(我可以仅使用html和php来做到这一点) - how to pass value to php thorough POST(Can i do this just with html and php) 如何在PHP中将值传递给FacebookRedirectLoginHelper - How can I pass value to FacebookRedirectLoginHelper in PHP 如何将输入从PHP脚本传递到另一个脚本? - How can I pass the input from a PHP script to another script? 如何使用HTML表单将JavaScript值传递到另一个PHP页面? - How can I pass JavaScript values to another PHP page using an HTML form? 如何使用ajax将参数从page1.html传递到server.php并将结果返回给page2.php? - How can I pass parameters from page1.html to server.php and return results to page2.php using ajax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM