Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将表单发布到自身的Post/Redirect/Get_Php_Post_Post Redirect Get - Fatal编程技术网

Php 将表单发布到自身的Post/Redirect/Get

Php 将表单发布到自身的Post/Redirect/Get,php,post,post-redirect-get,Php,Post,Post Redirect Get,我想在一个页面中实现Post/Redirect/Get模式,该页面将发布到自身 表单是我目前正在构建的MVC应用程序的一部分,表单字段是用php生成的 表格位于此处: 以下是“myaction”的相关代码(请注意,会话已经启动,因此我可以从$\u会话进行读写): 公共函数myaction(){ $login=$_POST['comment']; 如果(isset($comment)){ //这里我没有做任何真正的检查,因为我只是测试重定向。只是检查是否为注释文本输入提供了值。 $\会话['tes

我想在一个页面中实现Post/Redirect/Get模式,该页面将发布到自身

表单是我目前正在构建的MVC应用程序的一部分,表单字段是用php生成的

表格位于此处:

以下是“myaction”的相关代码(请注意,会话已经启动,因此我可以从$\u会话进行读写):

公共函数myaction(){
$login=$_POST['comment'];
如果(isset($comment)){
//这里我没有做任何真正的检查,因为我只是测试重定向。只是检查是否为注释文本输入提供了值。
$\会话['test']='SUCCESS';
//在此处插入注释(到某个数据库中)。
标题(“位置:http://site.com/mycontroller/myaction/", 302);
退出();
回声“
谢谢您的评论!
”; }否则{ //在这里出示表格。 //表单向其自身发布,因此操作如下所示: } }
上述代码工作正常,如果用户刷新或使用“后退”按钮,浏览器不会要求他重新发布数据

问题是我想在插入评论后显示一条消息,上面写着“感谢评论”。同时,如果他们提供了一条空评论,我想显示表单和一条消息,上面写着“请输入评论!”

我如何着手实施这两项要求


干杯:)

我想说以下几点:

header("Location: http://site.com/mycontroller/myaction/?aftercomment=true", 302);
在myaction脚本中:

if ($_GET['aftercomment']) echo('Thanks for comment!');
公共函数myaction(){
如果(isset($_GET['ok'])){
回声“
谢谢您的评论!
”; } $comment=$_POST['comment']; 如果(isset($comment)){ //这里我没有做任何真正的检查,因为我只是测试重定向。只是检查是否为注释文本输入提供了值。 $\会话['test']='SUCCESS'; //在此处插入注释(到某个数据库中)。 标题(“位置:http://site.com/mycontroller/myaction/?ok=1", 302); 退出(); 回声“
谢谢您的评论!
”; }否则{ echo“请输入评论!”; //在这里出示表格。 //表单向其自身发布,因此操作如下所示: } }
有3种可能的解决方案

  • 忘记PRG,选择AJAX
  • 正如赫希马尔所说
  • 将消息写入会话

  • 对于错误消息,您可以就地显示它,而无需重定向。它的缺点是,如果用户最终未能填写表单,则要求用户重新加载页面,但这通常被认为是可以忽略的

    如果使用
    标题(“位置:…”)
    执行重定向,则不会显示以前打印的内容。此外,如果您的
    echo
    退出之后,则显然不会打印任何内容。
    
    if ($_GET['aftercomment']) echo('Thanks for comment!');
    
    public function myaction(){
    
     if(isset($_GET['ok'])) {
        echo "<BR>Thanks for commenting! <BR>";
     }
    
     $comment = $_POST['comment'];
    
        if(isset($comment)){
           //I am not doing any real checking here as I am just testing the redirect. Just checking to see if a value was supplied to the comment text input.
           $_SESSION['test'] = 'SUCCESS';
    
           //Insert the comment here (into some database).
    
           header("Location: http://site.com/mycontroller/myaction/?ok=1", 302);
           exit();
    
           echo "<BR>Thanks for commenting! <BR>";
        }else{
           echo  "Please enter a comment!";
           //Show the form here.
           //The form posts to itself, so the action looks like: <form action="" method="POST">
        }
    }