POST / Redirect / GET和错误处理 [英] POST/Redirect/GET with Error handling

查看:172
本文介绍了POST / Redirect / GET和错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章的原因是我试图避免大多数浏览器在刷新表单时发出的表单重新提交错误。



以下示例:



用户在名为 signpetition.php 的页面上显示一个基本的HTML表单:

 < div id =sign-errorsstyle =display:none;> 
< / div>

< form action =method =post>
< input type =submitname =submit_buttonvalue =sign petition/>
< / form>

因此,用户点击 submit 按钮现在POST数据将被读取和验证。



signpetition.php



 <$ c ($ set($ _ POST ['submit_button']))
{
//做一些验证

if($ errors == true)
{
//在这里做什么?我想回到最后一页,但保留错误。我应该将它们存储在会话变量中,然后在使用后将它们取消设置?我觉得好像有一个更好的解决方案。

$ _SESSION ['error'] =错误;
header('Location:'。$ _SERVER ['REQUEST_URI'],true,303);
出口;

}
else
{
$ _SESSION ['success'] =您的请愿书已成功签名!;
header('Location:'。$ _SERVER ['REQUEST_URI'],true,303);
出口;
}
}
?>

所以,总结一下:


我想避免表单重新提交问题,这就是为什么我在代码中使用
重定向头



解决方案

如果用户被重定向,那么永远不应该有表单重新提交。


将它们重定向到 submitted.php 并使用以下内容:

<$ p $如果(isset($ _ SESSION ['error'])){echo'the error';}
else if(isset($ _ SESSION ['success'])){echo'您的请愿书已成功签署!');}
else {echo'unknown error。'; }


The reason for this post is that I am trying to avoid the "form re submission error" that most browsers give off when a form is refreshed.

Take the following example:

The user is on a page called signpetition.php and shown a basic HTML form:

<div id="sign-errors" style="display: none;">
</div>

<form action="" method="post">
    <input type="submit" name="submit_button" value="sign petition" />
</form>

So, the user clicks the submit button and now the POST data will be read and verified.

signpetition.php

<?php
    if(isset($_POST['submit_button']))
    {
        // Do some validation

        if($errors == true)
        {
          // What to do here? I want to go back to the last page, but keep the errors. Should I store them in a session variable and then unset them after their use? I feel as though there must be a better solution to this.

            $_SESSION['error'] = "the error";
            header('Location: '. $_SERVER['REQUEST_URI'] , true, 303);
        exit;

        }
        else
        {
            $_SESSION['success'] = "your petition was successfully signed!";
            header('Location: '. $_SERVER['REQUEST_URI'] , true, 303);
        exit;
        }
    }
        ?>

So, to sum things up:

I want to avoid the form re submission issue, which is why I'm using the header redirection in my code

解决方案

If the users are being redirected, then there should never be a form re-submission.

Redirect them to submitted.php and use the following:

if(isset($_SESSION['error'])){ echo'the error';}
else if(isset($_SESSION['success'])){ echo'your petition was successfully signed!');}
else{ echo 'unknown error.'; }

这篇关于POST / Redirect / GET和错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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