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


Messages In This Thread
Flashdata persists to second request - by El Forum - 11-11-2008, 09:47 AM
Flashdata persists to second request - by El Forum - 11-11-2008, 10:41 AM
Flashdata persists to second request - by El Forum - 11-11-2008, 11:13 AM



Theme © iAndrew 2016 - Forum software by © MyBB