如何从HTML表单运行PHP函数? [英] How to run a PHP function from an HTML form?

查看:260
本文介绍了如何从HTML表单运行PHP函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绝对是网络技术的初学者。我知道我的问题很简单,但我不知道该怎么做。
例如我有一个函数:

$ p $ 函数addNumbers($ firstNumber,$ secondNumber)
{
echo $ firstNumber + $ secondNumber;
}

我有一个表单:

 < form action =method =post> 
< p> 1号码:< input type =textname =number1/>< / p>
< p> 2号数字:< input type =textname =number2/>< / p>
< p>< input type =submit/>< / p>



我的文本字段,并通过按下我写入文本字段的参数来调用我的函数?
例如,我写5 - 第一个文本框,10秒的文本框,然后单击按钮,我在同一页上获得15个结果。
EDITED
我试过这么做:

  $ num1 = $ POST ['number1']; 
$ num2 = $ POST ['number2'];
addNumbers($ num1,$ num2);

但它不起作用,答案总是0。

解决方案

您拥有的功能是服务器端。服务器端代码在之前且仅在数据返回到浏览器之前运行(通常显示为页面,但也可以是 ajax请求)。



您的表单是客户端。此表单由您的浏览器呈现,并未连接到您的服务器,但可以将数据提交给服务器进行处理



因此,要运行该函数,必须发生以下流程:


  1. 服务器使用表单输出页面。无需进行服务器端处理。

  2. 浏览器加载该页面并显示表单。 用户将数据输入表单

  3. 用户按下提交按钮,使用数据向服务器发送 HTTP请求

  4. 处理请求的页面(可能与第一个请求相同)从请求中获取数据,运行您的函数,将结果输出到HTML页面。

示例PHP脚本完成所有这些工作:

<?php 

函数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>结果:<?php echo $ result?>< / h1>
<?php}?>
< form action =method =post>
< p> 1号码:< input type =textname =number1/>< / p>
< p> 2号数字:< input type =textname =number2/>< / p>
< p>< input type =submit/>< / p>

< / body>
< / html>

请注意:


  • 即使那些页面包含PHP和HTML代码,浏览器也不会知道PHP代码是什么。它看到的只是产生的HTML输出。 <?php ...?> 中的所有内容都由服务器执行(在本例中为 echo 创建此执行的唯一输出),而PHP标记外的所有内容—特别是HTML代码—直接输出到HTTP响应。

  • 您会注意到< h1>结果:... HTML代码是里面一个PHP 如果语句。这意味着这行不会在第一遍输出,因为没有 $ result

  • 因为表单 action 没有任何价值,表单会提交到浏览器已经在同一页面(URL)。


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. EDITED I've tried to do it so:

$num1 = $POST['number1'];
$num2 = $POST['number2'];
addNumbers($num1, $num2);

But it doesn't work, the answer is 0 always.

解决方案

The "function" you have is server-side. Server-side code runs before and only before data is returned to your browser (typically, displayed as a page, but also could be an ajax request).

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.

Therefore, to run the function, the following flow has to happen:

  1. Server outputs the page with the form. No server-side processing needs to happen.
  2. Browser loads that page and displays the form.
  3. User types data into the form
  4. User presses submit button, an HTTP request is made to your server with the data.
  5. 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 result into an HTML page.

Sample PHP script that does all of this:

<?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 inside a PHP if 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.

这篇关于如何从HTML表单运行PHP函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆