简体   繁体   English

使用$ _POST在同一页面上获取输入值

[英]Use $_POST to get input values on the same page

Sorry if this is a rather basic question. 对不起,如果这是一个相当基本的问题。

I have a page with an HTML form. 我有一个HTML表单的页面。 The code looks like this: 代码如下所示:

<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. 我想在同一页面上使用$ _POST变量。 How would I do this? 我该怎么做?

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. 它将以PHP的形式执行整个文件。 The first time you open it, $_POST['submit'] won't be set because the form has not been sent. 第一次打开它时,将不会设置$_POST['submit']因为表单尚未发送。 Once you click on the submit button, it will print the information. 单击提交按钮后,它将打印信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM