codeigniter flashdata

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    codeigniter flashdata

    Can anyone tell me why flashdata is not immediately available? Is it only available once the page has been resent? I am using form validation for form fields and flashdata for error messages. If there is an error I want the fields to be filled in with the $_POST data, which CI does easily, however, the flashdata seems to only be recognised once I refresh (through redirect()) which destroys the $_POST data.

    If I remove the redirect then validation errors and automatic re-entering of posted values works, but no flashdata message.

    controlller:
    Code:
    // Validation rules
    $this->form_validation->set_rules('name', 'username', 'required|alpha_dash');
    $this->form_validation->set_rules('pass', 'password', 'required|alpha_dash');
    
    // Check form has been submitted with valid form info
    if ($this->form_validation->run()) {
    	$name = $this->input->post('name');
    	$pass = $this->input->post('pass');
    	
    	...
    
    	// Return if name and password was not found
    	if ($check===FALSE) {
    		$this->session->set_flashdata('message_type', 'warning');
    		$this->session->set_flashdata('message', 'Username not found.');
    	// Success
    	} else {
    		...
    		$this->session->set_flashdata('message_type', 'information');
    		$this->session->set_flashdata('message', 'Logged in.');
    	}
    	// Success or fail redirect page
    	redirect('user/login'); exit();
    }
    // Load Page
    $this->load->view('v_user_login');
    view:
    Code:
    <?php if($this->session->flashdata('message')) : ?>
    	<div class="<?=$this->session->flashdata('message_type')?>">
    		<h3><span>&nbsp;</span><?=$this->session->flashdata('message')?></h3>
    	</div>
    <?php endif; ?>
    
    <h1>User Login</h1>
    <?=form_open('user/login')?>
    	<?=form_fieldset('Login Form')?>
    
    		<div class="textfield">
    			<?=form_label('Username','name')?>
    			<?=form_input('name', set_value('name'))?>
    			<?=form_error('name')?>
    		</div>
    		
    		<div class="textfield">
    			<?=form_label('Password', 'pass')?>
    			<?=form_password('pass')?>
    			<?=form_error('pass')?>
    		</div>
    
    		<div class="buttons">
    			<?=form_submit('login', 'Login')?>
    		</div>
    		
    	<?=form_fieldset_close()?>
    <?=form_close()?>
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Hi, The Servant - sorry for the late reply.

    Yes, flash data is only available to the *next* server request.

    For this instance, you might just set some dynamic object properties:

    Code:
    $this->message_type = ...
    /** ... */
    <?=$this->message_type;?>

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      Thanks mate.

      *************** ***********

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by TheServant
        Thanks mate.

        *************** ***********
        No problem, dude. BTW, to overcome that 'message too short' problem, just stick a lot of space between your words (the software isn't smart enough to strip it out - lol).

        Code:
        Thanks                                                   mate.

        Comment

        Working...