首页 > 解决方案 > 表单方法=发布和操作=index.php?page=somepage

问题描述

我正在编写一个在家庭服务器上运行的小脚本,它将通过 post 处理表单数据,但应该提交到带有获取值的页面。即 index.php 文件接收一个 $_GET ,它设置加载哪个页面。

如果页面 index.php?page=form 被加载,那么应该会出现一个表单,这很好,但表单处理在同一页面上。

有这样的表格:

<form action="index.php?page=form" method="post">
    <input type="text" name="email">
    Other form fields here
</form>

将数据发布到 index.php?而不是 index.php?page=form。

这可以做到吗?

标签: htmlforms

解决方案


上面的代码对我有用。如果我按下回车键,我会进入站点 index.php?page=form。也许您的问题在其他任何地方。这里有两个选项,也许其中一个对您有用...

这对你来说可能吗?

如果没有设置$_POST,就会显示表单,如果提交表单,就可以处理数据。所以你不再需要 get 变量了。

<?php if (isset($_POST['email'])): ?>
  <!-- process the inputs -->
<?php else: ?>
<form action="index.php" method="post">
  <input type="text" name="email">
  <!-- Other form fields here -->
  <button type="submit">Submit</button>
</form>
<?php endif; ?>

如果您需要 $_GET 变量,请尝试

<?php 
if ($_GET['page'] == 'form'):
  //process the inputs
else: ?>
<form action="index.php" method="GET">
  <input type="hidden" name="page" value="form">
  <input type="text" name="email">
  <!-- Other form fields here -->
  <button type="submit">Submit</button>
</form>
<?php endif; ?>

推荐阅读