PHP header()用POST变量重定向 [英] PHP header() redirect with POST variables

查看:158
本文介绍了PHP header()用POST变量重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP,并且正在制作表单发布到的操作页面。页面检查错误,然后如果一切正常,它会将它们重定向到发布数据的页面。如果没有,我需要将它们重定向回到它们出现错误的页面和POST变量。这是它的工作原理。

I'm working with PHP, and I'm making an action page which a form posts to. The page checks for errors, then if everything is fine, it redirects them to the page where the data has been posted. If not, I need to to redirect them back to the page they were at with an error and the POST variables. Here is the gist of how it works.

HTML看起来像这样...

The HTML would look like this...

<form name="example" action="action.php" method="POST">
  <input type="text" name="one">
  <input type="text" name="two">
  <input type="text" name="three">
  <input type="submit" value="Submit!">
</form>

action.php看起来像这样...

action.php would look like this...

if(error_check($_POST['one']) == true){
    header('Location: form.php');
    // Here is where I need the data to POST back to the form page.
} else {
    // function to insert data into database
    header('Location: posted.php');
}

在出现错误的情况下,我需要将它回发到第一个页。
我无法使用GET,因为输入过大。
如果可能,我不想使用SESSION。
这可能吗?

In the case of an error, I need it to POST back to the first page. I can't use GET, because the input will be too large. I don't want to use SESSION, if possible. Is this possible?

推荐答案

如果你不想使用会话,你可以做的唯一事情就是POST到同一页面。无论如何,哪个国际海事组织是最好的解决方案。

If you don't want to use sessions, the only thing you can do is POST to the same page. Which IMO is the best solution anyway.

// form.php

<?php

    if (!empty($_POST['submit'])) {
        // validate

        if ($allGood) {
            // put data into database or whatever needs to be done

            header('Location: nextpage.php');
            exit;
        }
    }

?>

<form action="form.php">
    <input name="foo" value="<?php if (!empty($_POST['foo'])) echo htmlentities($_POST['foo']); ?>">
    ...
</form>

这可以变得更加优雅,但你明白了......

This can be made more elegant, but you get the idea...

这篇关于PHP header()用POST变量重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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