发布数据和刷新页面 [英] Post data and refresh page

查看:91
本文介绍了发布数据和刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个编辑页面来编辑我的网站帖子。
它使用post方法到同一页面。
如果表单编译正确,则显示恭喜您的消息。



问题:

当用户点击刷新按钮时,脚本会尝试再次将数据重新发布到页面。
有没有办法避免这种情况?



谢谢

Luca

解决方案

PRG模式的大纲如下:

 <$ c ($ _SERVER ['REQUEST_METHOD'] =='POST')
{
///做你的魔法

$ _SESSION ['error'] =感谢您的留言!;

//这应该是每个规范的完整URL,但/yourscript.php可以工作
$ myurl = ...;

header(Location:$ myurl);
头(HTTP / 1.1 303 See Other);
die(重定向);


if(isset($ _ SESSION ['error']))
{
print提交结果:。$ _ SESSION ['error ];
unset($ _ SESSION ['error']);
}


I have an edit-form page to edit my website posts. It uses post method to the same page. If the form is compiled correctly shows up a congrats message.

The problem:

When users hit the refresh button the script tries to repost the data again to page. Is there a way to avoid this?

thanks

Luca

解决方案

The general outline of the PRG pattern is this:

if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
     /// do your magic

     $_SESSION['error'] = "Thanks for your message!";

     // this should be the full URL per spec, but "/yourscript.php" will work
     $myurl = ...;

     header("Location: $myurl");
     header("HTTP/1.1 303 See Other");
     die("redirecting");
}

if ( isset($_SESSION['error']) )
{
     print "The result of your submission: ".$_SESSION['error'];
     unset($_SESSION['error']);
}

这篇关于发布数据和刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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