title: Codeigniter - re-populating form on failed validation after submitting
body: <p>I have a form that requires the user to enter some information.  If they fail to complete the required fields they are re-presented with the form; the top of the page notifying them what fields are required and I've enabled sticky forms (set_value()) so their input is not lost.</p>
  
  <p>I'm using flashdata to display messages to the user (i.e., if what they've entered already exists in the database).  </p>
  
  <p>My form is in the index method of my controller.
  When submit is clicked from my view it calls the add() method in my controller.
  The add() method performs the validation and depending on the results either submits to the database or kicks back out to the user to get more data.</p>
  
  <p>I have several issues with the way that i've done this.
  1. If validation fails I'm using $this->index() to get back to my form and display the validation errors.  If I try using redirect, I lose my validation errors and my $_POST[] data so my sticky forms end up blank.
  2. Using $this->index() appends the 'add' to the end of my url
  3. Using $this->index() causes issues with the flashdata.  Random results.</p>
  
  <p>Any ideas?</p>
  
  <pre><code>&lt;?php
  class Restaurant extends Controller {
  
      function Restaurant() {
          parent::Controller();
      }
  
      function index() {
  
          // Load libraries and models
          $this-&gt;load-&gt;model('/restaurant/mRestaurantTypes');
          $this-&gt;load-&gt;model('/restaurant/mRestaurant');
          $this-&gt;load-&gt;model('/utilities/mUtilities');
  
          // Get states
          $stateSelect = array();
          $getStates = $this-&gt;mUtilities-&gt;getStates();
  
          if($getStates-&gt;num_rows() &gt; 0) {
              foreach($getStates-&gt;result() as $row) {
                  $stateSelect[$row-&gt;abbr] = $row-&gt;name;
              }
          }
  
  
          // Get restaurant types
          $restaurantTypes = array();
          $getRestaurantTypes = $this-&gt;mRestaurantTypes-&gt;getRestaurantTypes();
  
          if($getRestaurantTypes-&gt;num_rows() &gt; 0) {
              foreach($getRestaurantTypes-&gt;result() as $row) {
                  $restaurantTypes[$row-&gt;restaurant_types_id] = $row-&gt;type;
              }
          }
  
          // Create form elements
          $data['name'] = array(
              'name'      =&gt;  'name',
              'id'        =&gt;  'name',
              'value'     =&gt;  set_value('name'),
              'maxlength' =&gt;  '200',
              'size'      =&gt;  '50'
          );
  
          $data['address'] = array(
              'name'      =&gt;  'address',
              'id'        =&gt;  'address',
              'value'     =&gt;  set_value('address'),
              'maxlength' =&gt;  '200',
              'size'      =&gt;  '50'
          );
  
          $data['city'] = array(
              'name'      =&gt;  'city',
              'id'        =&gt;  'city',
              'value'     =&gt;  set_value('city'),
              'maxlength' =&gt;  '50',
              'size'      =&gt;  '25'        
          );
  
          $data['state'] = $stateSelect;
  
          $data['zip'] = array(
              'name'      =&gt;  'zip',
              'id'        =&gt;  'zip',
              'value'     =&gt;  set_value('zip'),
              'maxlength' =&gt;  '10',
              'size'      =&gt;  '10'    
          );
  
          $data['phone'] = array(
              'name'      =&gt;  'phone',
              'id'        =&gt;  'phone',
              'value'     =&gt;  set_value('phone'),
              'maxlength' =&gt;  '15',
              'size'      =&gt;  '15'    
          );
  
          $data['url'] = array(
              'name'      =&gt;  'url',
              'id'        =&gt;  'url',
              'value'     =&gt;  set_value('url'),
              'maxlength' =&gt;  '255',
              'size'      =&gt;  '50'    
          );
  
          $data['type'] = $restaurantTypes;
  
          $data['tags'] = array(
              'name'      =&gt;  'tags',
              'id'        =&gt;  'tags',
              'value'     =&gt;  set_value('tags'),
              'maxlength' =&gt;  '255',
              'size'      =&gt;  '50'    
          );
  
          $data['active'] = array(
              'name'      =&gt;  'active',
              'id'        =&gt;  'active',
              'value'     =&gt;  'Y',
              'maxlength' =&gt;  '1',
              'size'      =&gt;  '2' 
          );
  
          // Set page variables
          $data_h['title'] = "Add new restaurant";
  
          // Load views
          $this-&gt;load-&gt;view('/template/header', $data_h);
          $this-&gt;load-&gt;view('/restaurant/index', $data);
          $this-&gt;load-&gt;view('/template/footer');      
  
      }
  
  
      /**
       * Add the the new restaurant to the database.
       */
      function add() {
  
          // Load libraries and models
          $this-&gt;load-&gt;library('form_validation');
          $this-&gt;load-&gt;model('/restaurant/mRestaurant');
  
          // Define validation rules
          $this-&gt;form_validation-&gt;set_rules('name',       'Name',     'trim|required|max_length[255]|xss_clean');
          $this-&gt;form_validation-&gt;set_rules('address',    'Address',  'trim|required|max_length[100]|xss_clean');
          $this-&gt;form_validation-&gt;set_rules('city',       'City',     'trim|required|max_length[128]|xss_clean');
          //$this-&gt;form_validation-&gt;set_rules('state',        'State',    'trim|required');
          $this-&gt;form_validation-&gt;set_rules('zip',        'Zip',      'trim|required|max_length[128]|xss_clean');
          $this-&gt;form_validation-&gt;set_rules('phone',      'Phone',    'trim|required|max_length[10]|xss_clean');
          $this-&gt;form_validation-&gt;set_rules('url',        'URL',      'trim|required|max_length[255]|xss_clean');
          $this-&gt;form_validation-&gt;set_rules('tags',       'Tags',     'trim|xss_clean');
  
  
          // Form validation
          if ($this-&gt;form_validation-&gt;run() == FALSE) {
  
              // On failure
              $this-&gt;index();
  
          } else {
  
              // On success, prepare the data
              $data = array(
                  'name'      =&gt;  $_POST['name'],
                  'address'   =&gt;  $_POST['address'],
                  'city'      =&gt;  $_POST['city'],
                  'state'     =&gt;  $_POST['state'],
                  'zip'       =&gt;  $_POST['zip'],
                  'phone'     =&gt;  $_POST['phone'],
                  'url'       =&gt;  $_POST['url'],
                  'type'      =&gt;  $_POST['type'],
                  'tags'      =&gt;  $_POST['tags'],
                  'active'    =&gt;  $_POST['active'],
              );
  
              // Check if the restaurant already exists
              $check = $this-&gt;mRestaurant-&gt;getRestaurant($data['name'], $data['zip']);
  
              // If no records were returned add the new restaurant
              if($check-&gt;num_rows() == 0) {
                  $query = $this-&gt;mRestaurant-&gt;addRestaurant($data);
  
                  if ($query) {
                      // On success
                      $this-&gt;session-&gt;set_flashdata('status', '&lt;div class="success"&gt;Added New Restaurant!&lt;/div&gt;');
                  } else {
                      // On failure
                      $this-&gt;session-&gt;set_flashdata('status', '&lt;div class="error"&gt;Could not add a new restaurant.&lt;/div&gt;');    
                  }
  
                  redirect('restaurant/confirm', 'refresh');
              } else {
                  // Notify the user that the restaurant already exists in the database
                  $this-&gt;session-&gt;set_flashdata('status', '&lt;div class="notice"&gt;This restaurant already exists in the database.&lt;/div&gt;');
                  redirect('restaurant/index');
              }
  
          }
  
      }
  
  
      function confirm() {
  
          $data['title'] = "Confirm";
  
          $this-&gt;load-&gt;view('/template/header');
          $this-&gt;load-&gt;view('/restaurant/confirm', $data);
          $this-&gt;load-&gt;view('/template/footer');
      }
  }
  ?&gt;
  </code></pre>