title: php - codeigniter ajax form validation
body: <p>Hi I’m quite new to jquery -ajax and I’d like some help please to join it with CI.</p>
  
  <p>I have followed this tutorial on <a href="http://www.youtube.com/watch?v=GrycH6F-ksY" rel="nofollow">Submitting a Form with AJAX</a> and I’d like to add this functionality to my CodeIgniter site. What I’d like to do is when the user submits the form, if there are any validation errors to show the individually on each input field (as in native ci process), or if this is not possible via validation_errors() function.  If no errors occured to display a success message above the form. </p>
  
  <p>Here's my code so far:</p>
  
  <p>my view</p>
  
  <pre><code> // If validation succeeds then show a message like this, else show errors individually or in validation_errors() in a list
  &lt;div class="alert alert-success"&gt;Success!&lt;/div&gt;
  &lt;?php echo validation_errors(); //show all errors that ajax returns here if not individualy  ?&gt;
  
  &lt;?php echo form_open('admin/product/add, array('class' =&gt; 'ajax-form')); ?&gt;
  &lt;p&gt;
      &lt;label for="product_name"&gt;Product *&lt;/label&gt;
      &lt;input type="text" name="product_name" value="&lt;?php echo set_value('product_name', $prod-&gt;product_name); ?&gt;" /&gt;
      &lt;?php echo form_error('product_name'); ?&gt;
  &lt;/p&gt;
  &lt;p&gt;
      &lt;label for="brand"&gt;Brand&lt;/label&gt;
      &lt;input type="text" name="brand" value="&lt;?php echo set_value('brand', $prod-&gt;brand); ?&gt;" /&gt;
      &lt;?php echo form_error('brand'); ?&gt;
  &lt;/p&gt;
  ...  
  </code></pre>
  
  <p>my controller</p>
  
  <pre><code> public function add($id){
             // set validation rules in CI native
             $rules = $this-&gt;product_model-&gt;rules;
             $this-&gt;form_validation-&gt;set_rules($rules);
  
             if ($this-&gt;form_validation-&gt;run() === true) {
                         // get post data and store them in db
            $data = $this-&gt;input_posts(array('product_name', 'brand', 'category_id', 'description'));
            $this-&gt;product_model-&gt;save($data, $id);
      // no errors - data stored - inform the user with display success-div
       } else {
      // validation failed - inform the user by showing the errors
       }
  //load the view
  $this-&gt;load-&gt;view('admin/products/add', $data);
  }  
  </code></pre>
  
  <p>and here’s the js script</p>
  
  <pre><code> $(document).ready(function () {
   $('form.ajax-form').on('submit', function() {
    var obj = $(this), // (*) references the current object/form each time
     url = obj.attr('action'),
     method = obj.attr('method'),
     data = {};
  
    obj.find('[name]').each(function(index, value) {
     // console.log(value);
     var obj = $(this),
      name = obj.attr('name'),
      value = obj.val();
  
     data[name] = value;
    });
  
    $.ajax({
     // see the (*)
     url: url,
     type: method,
     data: data,
     success: function(response) {
      console.log(response); // how to output success or the errors instead??
     }
    });
    return false; //disable refresh
   });
  });  
  </code></pre>
  
  <p>How should I pass my validation results (either success or the post errors) throught the ajax request and display them on my view?? 
  From some little research I did I've found that you can use a single controller, that holds both the native proccess and the ajax request (instead of using 2 controllers), but my main difficulty is, I don't understand how the results of the validation will pass through the js script and display them on my view?? Please note that I don't want to display anything on an alert box, instead show the results on a div or the errors individualy(if possible).</p>
  
  <p><strong>EDIT</strong> I did some changes to my application, here's the code so far:</p>
  
  <p>the controller</p>
  
  <pre><code>public function manage($id = NULL){
      $this-&gt;load-&gt;library('form_validation');
  
      $data['categ'] = $this-&gt;category_model-&gt;with_parents();
  
      //fetch a single product or create(initialize inputs empty) a new one
      if (isset($id) === true) {
          $data['prod'] = $this-&gt;product_model-&gt;get($id);
          $data['attr'] = $this-&gt;attribute_model-&gt;get_by('product_id', $id, null, true);
      } else {
          $data['prod'] = $this-&gt;product_model-&gt;make_new();
          $data['attr'] = $this-&gt;attribute_model_model-&gt;make_new();
      }
  
      if (isset($_POST['general_settings'])) {
          if ($this-&gt;form_validation-&gt;run('product_rules') === true) {
              // get post inputs and store them in database
              $data = $this-&gt;product_model-&gt;input_posts(array('product_name', 'brand', 'category_id', 'general_description'));
              $this-&gt;product_model-&gt;save($data, $id);
  
              $status = true;
          } else {
              // validation failed
              $status = validation_errors();
          }
          if ( $this-&gt;input-&gt;is_ajax_request() ) {
              echo json_encode($status);
              exit;
          }
          redirect('admin/product');
      }
      //if (isset($_POST['attributes_settings'])) { the same thing here  }                
  
      // load the view
      $this-&gt;load-&gt;view('admin/products/manage', $data);
  }
  </code></pre>
  
  <p>and the js</p>
  
  <pre><code>success: function(response) {
      //console.log(response);
      if (data.status === true) {
          $('#ajaxResults').addClass('alert alert-success').html(response);
      } else {
          $('#ajaxResults').addClass('alert alert-error').html(response);
      };
  
  }
  </code></pre>
  
  <p>But I'm having some issues</p>
  
  <ol>
  <li>Although I get the error messages from validation_errors() as an alert-error when there are no errors I get the true in an alert-error too, insted of alert-success. </li>
  </ol>
  
  <p>2.how should I return the success message too? eg. a message saying "Saves were done!". </p>
  
  <ol>
  <li>Althought in a non-ajax-request the data are stored in the database, in case fo ajax the don't store. Any ideas What may be wrong???</li>
  </ol>