开发者

Flashdata only shows every OTHER time the code runs

I am relatively new to CodeIgniter, so I'm not sure if this is just bad coding, or if it is a problem with how I'm using CodeIgniter's flash data. For context: the user submits a phrase in a simple HTML form. The phrase is compared against what should be typed in (pretty simple, right?). This correct phrase changes based upon what step in the activity they are on. When they get the text wrong, I am attempting to use flashdata to show the error message. Here are the controller portions, followed by the view:

//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->ses开发者_如何学编程sion->set_flashdata('entry_error', '<b>Sorry!</b>Your entry was incorrect. Be sure to carefully read the instructions!');
                $this->load->view('template', $data);
        }

Here is the view that only runs every other time.

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


From the docs: CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.

You are setting the flashdata and then trying to access it during the same request. It's not available until the next request which is why it seems like it's only working every other time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜