如何从 HTML 表单运行 PHP 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5968280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a PHP function from an HTML form?
提问by Daria
I'm absolute beginner in web technologies. I know that my question is very simple, but I don't know how to do it. For example I have a function:
我绝对是网络技术的初学者。我知道我的问题很简单,但我不知道该怎么做。例如我有一个功能:
function addNumbers($firstNumber, $secondNumber)
{
echo $firstNumber + $secondNumber;
}
And I have a form:
我有一个表格:
<form action="" method="post">
<p>1-st number: <input type="text" name="number1" /></p>
<p>2-nd number: <input type="text" name="number2" /></p>
<p><input type="submit"/></p>
How can I input variables on my text fields and call my function by button pressing with arguments that I've wrote into text fields? For example I write 5 - first textfield, 10 - second textfield, then I click button and I get the result 15 on the same page. EDITEDI've tried to do it so:
如何在我的文本字段中输入变量并通过按下我已写入文本字段的参数的按钮来调用我的函数?例如,我写 5 - 第一个文本字段,10 - 第二个文本字段,然后单击按钮,在同一页面上得到结果 15。 编辑我试过这样做:
$num1 = $POST['number1'];
$num2 = $POST['number2'];
addNumbers($num1, $num2);
But it doesn't work, the answer is 0 always.
但它不起作用,答案总是0。
回答by Nicole
The "function" you have is server-side. Server-side code runs before and only beforedata is returned to your browser (typically, displayed as a page, but also could be an ajax request).
您拥有的“功能”是server-side。服务器端代码仅在数据返回到浏览器之前运行(通常显示为页面,但也可以是ajax 请求)。
The form you have is client-side. This form is rendered by your browser and is not "connected" to your server, but can submit data to the server for processing.
您拥有的表单是client-side。此表单由您的浏览器呈现,并未“连接”到您的服务器,但可以将数据提交给服务器进行处理。
Therefore, to run the function, the following flow has to happen:
因此,要运行该函数,必须发生以下流程:
- Server outputs the page with the form. No server-side processing needs to happen.
- Browser loads that page and displays the form.
- User types data into the form
- User presses submit button, an HTTP requestis made to your server with the data.
- The page handling the request (could be the same as the first request) takes the data from the request, runs your function, and outputs the resultinto an HTML page.
- 服务器输出带有表单的页面。不需要进行服务器端处理。
- 浏览器加载该页面并显示表单。
- 用户在表单中键入数据
- 用户按下提交按钮,将向您的服务器发送包含数据的HTTP 请求。
- 处理请求的页面(可能与第一个请求相同)从请求中获取数据,运行您的函数,并将结果输出到 HTML 页面中。
Here is a sample PHP script which does all of this:
这是一个示例 PHP 脚本,它执行所有这些操作:
<?php
function addNumbers($firstNumber, $secondNumber) {
return $firstNumber + $secondNumber;
}
if (isset($_POST['number1']) && isset($_POST['number2'])) {
$result = addNumbers(intval($_POST['number1']), intval($_POST['number2']));
}
?>
<html>
<body>
<?php if (isset($result)) { ?>
<h1> Result: <?php echo $result ?></h1>
<?php } ?>
<form action="" method="post">
<p>1-st number: <input type="text" name="number1" /></p>
<p>2-nd number: <input type="text" name="number2" /></p>
<p><input type="submit"/></p>
</body>
</html>
Please note:
请注意:
- Even those this "page" contains both PHP and HTML code, your browser never knows what the PHP code was. All it sees is the HTML output that resulted. Everything inside
<?php ... ?>
is executed by the server(and in this case,echo
creates the only output from this execution), while everything outside the PHP tags — specifically, the HTML code — is output to the HTTP Response directly. - You'll notice that the
<h1>Result:...
HTML code is insidea PHPif
statement. This means that this line will not be output on the first pass, because there is no$result
. - Because the form
action
has no value, the form submits to the same page (URL) that the browser is already on.
- 即使这个“页面”同时包含 PHP 和 HTML 代码,您的浏览器也永远不知道 PHP 代码是什么。它看到的只是结果的 HTML 输出。内部的所有
<?php ... ?>
内容都由服务器执行(在这种情况下,echo
创建此执行的唯一输出),而 PHP 标记之外的所有内容(特别是 HTML 代码)直接输出到 HTTP 响应。 - 您会注意到
<h1>Result:...
HTML 代码位于PHPif
语句中。这意味着第一遍不会输出该行,因为没有$result
. - 因为表单
action
没有值,所以表单提交到浏览器所在的页面(URL)。
回答by Dave Kiss
You need to gather the values from the $_POST
variable and pass them into the function.
您需要从$_POST
变量中收集值并将它们传递给函数。
if ($_POST) {
$number_1 = (int) $_POST['number1'];
$number_2 = (int) $_POST['number2'];
echo addNumbers($number_1, $number_2);
}
Be advised, however, that you shouldn't trust user input and thus need to validate and sanitize your input.
但是,请注意,您不应该信任用户输入,因此需要验证和清理您的输入。
回答by mr_eclair
Try This.
尝试这个。
<?php
function addNumbers($firstNumber, $secondNumber)
{
if (isset($_POST['number1']) && isset($_POST['number2']))
{
$firstNumber = $_POST['number1'];
$secondNumber = $_POST['number2'];
$result = $firstNumber + $secondNumber;
echo $result;
}
}
?>
<form action="urphpfilename.php" method="post">
<p>1-st number: <input type="text" name="number1" /></p>
<p>2-nd number: <input type="text" name="number2" /></p>
<?php addNumbers($firstNumber, $secondNumber);?>
<p><?php echo $result; ?></p>
<p><input type="submit"/></p>
回答by Rene Pot
The variables will be in the $_POST variable.
变量将在 $_POST 变量中。
To parse it to the function you need to do this:
要将其解析为您需要执行此操作的函数:
addNumbers($_POST['number1'],$_POST['number2']);
Be sure you check the input, users can add whatever they want in it. For example use is_numeric() function
请务必检查输入,用户可以在其中添加任何他们想要的内容。例如使用 is_numeric() 函数
$number1 = is_numeric($_POST['number1']) ? $_POST['number1'] : 0;
Also, don't echo inside a function, better return it:
另外,不要在函数内部回显,最好返回它:
function addNumbers($firstNumber, $secondNumber)
{
return $firstNumber + $secondNumber;
}
// check if $_POST is set
if (isset($_POST['number1']) && isset($_POST['number2']))
{
$number1 = is_numeric($_POST['number1']) ? $_POST['number1'] : 0;
$number2 = is_numeric($_POST['number2']) ? $_POST['number2'] : 0;
echo addNumbers($_POST['number1'],$_POST['number2']);
}
回答by Cups
You are missing the underscores in
你缺少下划线
$_POST['number1']
That's all.
就这样。