Welcome Guest, Not a member yet? Register   Sign In
Flashdata persists to second request
#1

[eluser]beachbum[/eluser]
Hello,

I'm relatively new to Code Igniter and have been using it to perform some simple form processing. I may have made an error in my code that caused this problem, but I've been unable to find it so far.

This is my controller function, which takes the POSTed form data, validates it, and then sets either an "error" or a "success" message in the flashdata based on the outcome of the input validation.

However, the flashmessage does not appear until the second request after the form has been posted, and if the form has been posted more than once, the old flashdata persists. Can anyone please offer some advice? Thanks!

Code:
function send()
    {
         $data['title'] = 'Contact Us | American Retina Foundation';
        
          if ( $_POST['submit'] != '' )
          {
              $this->load->library( 'form_validation' );
              
              $this->form_validation->set_rules( 'first_name', 'First name', 'trim|required' );
               $this->form_validation->set_rules( 'last_name', 'Last name', 'trim|required' );
               $this->form_validation->set_rules( 'email', 'Email address', 'trim|required|valid_email' );
               $this->form_validation->set_rules( 'message', 'Message', 'trim|required' );
              
              if ( $this->form_validation->run() == FALSE )
              {
                   $this->session->set_flashdata( 'error', validation_errors() );
              }
              else
              {
                    // mail( $staff, $subject, $message, $headers );
                    
                    // insert contact record into db
                    $sender = array(
                                   'first_name'   => $this->input->post( 'first_name' ),
                                   'last_name'    => $this->input->post( 'last_name' ),
                                   'email'        => $this->input->post( 'email' ),
                                   'phone'        => $this->input->post( 'phone' ),
                                   'message'      => $this->input->post( 'message' ),
                                   'sent'         => NULL,
                                   'ip'           => $this->input->ip_address(),
                                   'user_agent'   => $this->input->user_agent()
                                );

                    $this->db->insert('tblContacts', $sender);

                   // display success notice
                   $this->session->set_flashdata( 'success', 'Thank you! Your message has been sent.' );
              }          
          }
        
         $data['section']              = 'contact';
        $data['content_for_layout']   = $this->load->view( 'contact/index', '', TRUE );
        $this->load->view( 'layouts/secondary', $data );
    }

Here is the form itself: Contact Form
#2

[eluser]narkaT[/eluser]
instead of using flashdata, why don't you just save the result in you $data variable?

session data added or updated "in the current request" is only available
on the next request.
you'll have to "redirect()" before you can access the flashdata. Wink
#3

[eluser]beachbum[/eluser]
Thanks - that's the info I needed.

I tried using redirect(), but that interfered with the set_value() function of the form_validation library, so I just put the messages in the $data array.




Theme © iAndrew 2016 - Forum software by © MyBB