我执行Post/Redirect/Get模式的问题 [英] Problem with my implementation of the Post/Redirect/Get pattern

查看:79
本文介绍了我执行Post/Redirect/Get模式的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是在表单上使用 Post/Redirect/Get 方法.我的表格通常总是向自己提交.但是,当我在表单中有错误时,我不会发送任何新的标题.这样我就可以轻松地做这样的事情

I always use the Post/Redirect/Get method on my forms. My forms generally always submit to themselves. However, when I have an error in the form, I don't send any new headers. This is so I can easily do stuff like this

<input type="text" name="email" value="<?php echo $this->input->post('email', '') ?>" />

PHP只是一个处理两个参数的库函数,$ _ POST键和一个默认值(如果不存在的话).

The PHP is just a library function that handles 2 arguments, the $_POST key and a default value if it isn't present.

这意味着,如果有人在表格中出错,则不必再次填写表格.缺点是重新加载页面会在浏览器中向他们发出POST警告.

This means if someone makes an error in the form, they don't have to refill out the form a second time. The disadvantage is that reloading the page gives them the POST warning in their browser.

总有办法避免这种情况,而无需使用某些状态(例如cookie,会话,数据库等)

Is there anyway to avoid this, without using something for state (i.e. cookies, session, database etc)

推荐答案

我发现做到这一点的最佳方法是使用标头函数.您可以将文件发布到所需的任何文件,甚至文件本身也可以进行验证,然后在失败时使用标头重定向返回到表单.将张贴的值存储在会话中或其他可访问的变量中,因此您可以访问先前输入的数据.

I find the best way to do this is use the header function. You can post to what ever file you need even itself do the validation then use the header redirect to go back to the form if it failed. Store the post'd values in the session or another accessible variable, therefore you can access the previously entered data.

在使用header("location:myscript.php")时;确保包括一个exit();之后,否则您仍然会在刷新时收到POST警告.

When using header("location: myscript.php"); make sure to include an exit(); afterwards otherwise you will still get the POST warning on a refresh.

myscript.php

myscript.php

if($_POST['submit'])
{
    //check for errrors

    if ($error)
    {
        $_SESSION['myPostVars'] = $_POST;
        header("location: myscript.php");
        exit();
    }
}

<form>
    // your form code
</form>

我刚刚注意到您已编辑问题,以避免使用会话.

I just noticed that you edited your question to avoid using sessions.

您可以序列化要返回的post var,并将其放入查询字符串中(通过header()发送

You could serialize the post vars you want to return and put them in the query string (sent via the header()

这篇关于我执行Post/Redirect/Get模式的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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