使用$ _POST在同一页面上获取输入值 [英] Use $_POST to get input values on the same page

查看:111
本文介绍了使用$ _POST在同一页面上获取输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,这是一个很基本的问题.

Sorry if this is a rather basic question.

我有一个带有HTML表单的页面.代码如下:

I have a page with an HTML form. 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>

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

Then in my file submit.php, I have the following:

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

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

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?

推荐答案

将其放在php文件中:

Put this on a php file:

<?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'],因为尚未发送该表单. 单击提交按钮后,它将打印信息.

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.

这篇关于使用$ _POST在同一页面上获取输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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