【问题标题】:Use $_POST to get input values on the same page使用 $_POST 在同一页面上获取输入值
【发布时间】:2013-01-30 02:41:49
【问题描述】:

对不起,如果这是一个相当基本的问题。

我有一个带有 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>

然后在我的文件submit.php 中,我有以下内容:

<?php
  $example = $_POST['example'];
  $example2 = $_POST['example2'];
  echo $example . " " . $example2;
?>

但是,我想消除对外部文件的使用。我想要同一页面上的 $_POST 变量。我该怎么做?

【问题讨论】:

标签: php


【解决方案1】:

把它放在一个 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>

它将整个文件作为 PHP 执行。第一次打开时,$_POST['submit'] 不会被设置,因为表单还没有发送。 单击提交按钮后,它将打印信息。

【讨论】:

猜你喜欢
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
  • 2021-06-27
  • 1970-01-01
相关资源
最近更新 更多