Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.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 如何防止在Zend framework中刷新页面时提交表单?_Php_Post_Zend Framework2_Page Refresh_Unset - Fatal编程技术网

Php 如何防止在Zend framework中刷新页面时提交表单?

Php 如何防止在Zend framework中刷新页面时提交表单?,php,post,zend-framework2,page-refresh,unset,Php,Post,Zend Framework2,Page Refresh,Unset,这是我对控制器的操作 public function invite() { if($this->getRequest()->isPost()){ $data_array = $this->getAllParams(); $emailList = explode(",", $data_array['emails']); $m

这是我对控制器的操作

public function invite()
{
    if($this->getRequest()->isPost()){
        $data_array                         = $this->getAllParams();
        $emailList                          = explode(",", $data_array['emails']);  
        $message                            = $data_array['message'];

        $error_emails                       = array();


        if(sizeof($emailList) <= 1){
            $this->view->message        = '<div class="alert alert-danger"> Please enter more than one email address and please separate emails by a "," (comma)</div>';    
        }
        else{
            $x = 0;
            foreach($emailList as $singleEmail){

                if (!filter_var(trim($singleEmail), FILTER_VALIDATE_EMAIL)) {
                    $error_emails[]             = $singleEmail;
                }
                else{
                    //send emails here
                    $x++;
                }

            }

            if(sizeof($error_emails) > 0){
                $email_str = implode(",",$error_emails);
                if($x > 0 ){
                    $this->view->message    = '<div class="alert alert-success"> Successfully Sent! </div> ';
                }
                $this->view->message        .= '<br /><div class="alert alert-danger"> Message to this emails were not sent! <b>'.$email_str.'</b> <br /> Please enter valid emails!</div>';    
            }else{
                $this->view->message        = '<div class="alert alert-success"> Successfully Sent! </div>';    
            }
        }
        $request = $this->getRequest();
        $request->setParam('emails', null);
        $request->setParam('message', null);            
    }

}
公共功能邀请()
{
如果($this->getRequest()->isPost()){
$data_array=$this->getAllParams();
$emailList=explode(“,”,$data_数组['emails']);
$message=$data_数组['message'];
$error_emails=array();
如果(sizeof($emailList)view->message='请输入多个电子邮件地址,并用“,”(逗号)”分隔电子邮件;
}
否则{
$x=0;
foreach($emailList作为$singleEmail){
如果(!filter_var(trim($singleEmail),filter_VALIDATE_EMAIL)){
$error_email[]=$singleEmail;
}
否则{
//在这里发送电子邮件
$x++;
}
}
如果(sizeof($error_emails)>0){
$email\u str=内爆(“,”,$error\u emails);
如果($x>0){
$this->view->message='Successfully Sent!';
}
$this->view->message.='
此邮件的邮件未发送!'.$email_str.
请输入有效邮件!'; }否则{ $this->view->message='Successfully Sent!'; } } $request=$this->getRequest(); $request->setParam('emails',null); $request->setParam('message',null); } }
我尝试了我找到的解决方案; 如您所见,我尝试了setParam方法将值设置为null。不幸的是,这不起作用。 另外,unset数组不起作用-不确定我是否做对了。
无论如何,我不想重定向。只想取消设置。有人能帮我吗?我已经尝试了几个小时了。提前谢谢。

这个问题不是Zend Framework特有的,有一种称为PRG(Post redirect Get的缩写)的模式来处理表单的重新提交:

Zend框架提供了PRG控制器插件,简而言之,它在会话中存储post数据并发出重定向,然后在后续get请求中返回存储的post数据

示例代码建议您在PRG插件重定向后处理GET请求表单:

// Pass in the route/url you want to redirect to after the POST
$prg = $this->prg('/user/register', true);

if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
    // returned a response to redirect us
    return $prg;
} elseif ($prg === false) {
    // this wasn't a POST request, but there were no params in the flash messenger
    // probably this is the first time the form was loaded
    return array('form' => $form);
}

// $prg is an array containing the POST params from the previous request
$form->setData($prg);

if($form->isValid()) {
   ...
但我不同意这种方法,并建议始终在POST上处理表单,并在事后使用PRG显示带有填充数据和验证消息的表单(注意
$form
这里是
Zend\form
的一个实例):


但是,由于您不使用表单,您将需要手动验证数据两次。一次用于处理,一次用于显示错误消息。如果您不想使用Zend\Form组件,我建议您查看Zend\InputFilter以验证输入。

此问题不特定于Zend Framework,有一种称为PRG的模式(Post Redirect Get的缩写)处理表单的重新提交:

Zend框架提供了PRG控制器插件,简而言之,它在会话中存储post数据并发出重定向,然后在后续get请求中返回存储的post数据

示例代码建议您在PRG插件重定向后处理GET请求表单:

// Pass in the route/url you want to redirect to after the POST
$prg = $this->prg('/user/register', true);

if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
    // returned a response to redirect us
    return $prg;
} elseif ($prg === false) {
    // this wasn't a POST request, but there were no params in the flash messenger
    // probably this is the first time the form was loaded
    return array('form' => $form);
}

// $prg is an array containing the POST params from the previous request
$form->setData($prg);

if($form->isValid()) {
   ...
但我不同意这种方法,并建议始终在POST上处理表单,并在事后使用PRG显示带有填充数据和验证消息的表单(注意
$form
这里是
Zend\form
的一个实例):


但由于您不使用表单,您将需要手动验证数据两次。一次用于处理,一次用于显示错误消息。如果您不想使用Zend\Form组件,我建议查看Zend\InputFilter以验证输入。

您可以在Zend中使用CSRF保护,即在提交表单时使用令牌。

您可以使用Czend ie中的SRF保护在提交表单时使用令牌

尝试标题并强制重新验证。会话/令牌也是另一个选项。避免重定向的原因是什么?为什么使用html字符串而不是控制器插件FlashMessenger?尝试标题并强制重新验证。会话/令牌也是另一个选项。什么这是您避免重定向的原因吗?为什么使用html的字符串而不是控制器插件FlashMessenger?