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

查看:48
本文介绍了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');
}

如果出现错误,我需要将其 POST 回第一页.我不能使用 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 到同一页面.无论如何,哪个 IMO 是最好的解决方案.

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天全站免登陆