php 使用 $_POST 获取同一页面上的输入值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14595810/
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
Use $_POST to get input values on the same page
提问by Piccolo
Sorry if this is a rather basic question.
对不起,如果这是一个相当基本的问题。
I have a page with an HTML form. The code looks like this:
我有一个带有 HTML 表单的页面。代码如下所示:
<form action="submit.php" method="post">
  Example value: <input name="example" type="text" />
  Example value 2: <input name="example2" type="text" />
  <input type="submit" />
</form>
Then in my file submit.php, I have the following:
然后在我的文件中submit.php,我有以下内容:
<?php
  $example = $_POST['example'];
  $example2 = $_POST['example2'];
  echo $example . " " . $example2;
?>
However, I want to eliminate the use of the external file. I want the $_POST variables on the same page. How would I do this?
但是,我想消除外部文件的使用。我想要在同一页面上的 $_POST 变量。我该怎么做?
回答by KaeruCT
Put this on a php file:
把它放在一个 php 文件中:
<?php
  if (isset($_POST['submit'])) {
    $example = $_POST['example'];
    $example2 = $_POST['example2'];
    echo $example . " " . $example2;
  }
?>
<form action="" method="post">
  Example value: <input name="example" type="text" />
  Example value 2: <input name="example2" type="text" />
  <input name="submit" type="submit" />
</form>
It will execute the whole file as PHP. The first time you open it, $_POST['submit']won't be set because the form has not been sent.
Once you click on the submit button, it will print the information.
它会将整个文件作为 PHP 执行。第一次打开时,$_POST['submit']不会设置,因为表单尚未发送。单击提交按钮后,它将打印信息。

