Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.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 Flashdata仅在代码每隔运行一次时显示_Php_Function_Codeigniter - Fatal编程技术网

Php Flashdata仅在代码每隔运行一次时显示

Php Flashdata仅在代码每隔运行一次时显示,php,function,codeigniter,Php,Function,Codeigniter,我对CodeIgniter比较陌生,所以我不确定这是不是糟糕的编码,或者这是我如何使用CodeIgniter的flash数据的问题。对于上下文:用户以简单的HTML形式提交短语。这个短语与应该输入的内容进行了比较(非常简单,对吧?)。这个正确的短语会根据他们所处的活动步骤而变化。当他们收到错误的文本时,我试图使用flashdata显示错误消息。以下是控制器部分,然后是视图: //Get step number $step = $this->input->post('step'); $

我对CodeIgniter比较陌生,所以我不确定这是不是糟糕的编码,或者这是我如何使用CodeIgniter的flash数据的问题。对于上下文:用户以简单的HTML形式提交短语。这个短语与应该输入的内容进行了比较(非常简单,对吧?)。这个正确的短语会根据他们所处的活动步骤而变化。当他们收到错误的文本时,我试图使用flashdata显示错误消息。以下是控制器部分,然后是视图:

//Get step number
$step = $this->input->post('step');
$correct_text = array(
1 => 'TESTPHRASE',...
...

//If user enters the correct text           
$entered_text = strtoupper($this->input->post('entered_text'));
        if ($entered_text == $correct_text[$step]) 
        {   
            ...
        }
        //If user enters the incorrect text
        else 
        {
                $data['step'] = $step;
                $this->session->set_flashdata('entry_error', '<b>Sorry!</b>Your entry was incorrect. Be sure to carefully read the instructions!');
                $this->load->view('template', $data);
        }
//获取步骤编号
$step=$this->input->post('step');
$correct\u text=数组(
1=>TESTPHRASE',。。。
...
//如果用户输入正确的文本
$entered_text=strtoupper($this->input->post('entered_text'));
如果($entered_text==$correct_text[$step])
{   
...
}
//如果用户输入的文本不正确
其他的
{
$data['step']=$step;
$this->session->set_flashdata('entry_error','Sorry!您的输入不正确。请务必仔细阅读说明!');
$this->load->view('template',$data);
}
这里的视图只在每隔一次运行一次

<?php 
if ($this->session->flashdata('entry_error'))
{ ?>
   <div id="game_error">
   <?php echo $this->session->flashdata('entry_error'); ?>
   </div>
<?php } ?>

来自:CodeIgniter支持“flashdata”或会话数据,这些数据仅可用于下一个服务器请求,然后自动清除


您正在设置flashdata,然后在同一请求期间尝试访问它。它在下一个请求之前不可用,这就是为什么它似乎每隔一次才工作。

这是下一个服务器请求。过程是:控制器调用“template.php”,同时加载“entry.php”显示错误的位置。如果一个视图调用另一个视图时出现问题,为什么会显示flashdata?您正在设置flashdata,然后立即加载
template.php
template.php
然后加载
entry.php
。这一切都发生在同一个请求期间。从另一个视图加载视图不需要es不会发出另一个请求,这都是在服务器上完成的。flashdata的一个示例用法可能是用户登录,设置flashdata,然后将其重定向到其他页面。当他们到达最后一个页面时,会显示一条存储在flashdata中的欢迎消息。后续页面加载不会显示welcome message.Ahh.好的,谢谢你在回答中的澄清。现在有意义了。再次感谢!@BlackPlanet:如果你
redirect()
current\u url()
设置flashdata后,它会工作,只需移动你的
load->view()
到最后一行,并确保在运行
if
块之前检查post数据是否存在。在任何情况下,设置变量也可以正常工作,正如您决定的那样。