title: Upload image using ajax in codeigniter upload library
body: <p>Hi im trying to upload a single image using ajax but I've got an error like this</p>
  
  <pre><code>&lt;p&gt;Severity: Warning&lt;/p&gt;
   &lt;p&gt;Message:  Illegal offset type in isset or empty&lt;/p&gt;
  &lt;p&gt;Filename: core/Input.php&lt;/p&gt;
  &lt;p&gt;Line Number: 196&lt;/p&gt;
  </code></pre>
  
  <p>here is my code in view <strong>slider.php</strong></p>
  
  <pre><code>//script
  
   function save()
      {
      $('#btnSave').text('saving...'); //change button text
      $('#btnSave').attr('disabled',true); //set button disable 
      var url;
  
      if(save_method == 'add') {
       url = "&lt;?php echo site_url('slider/ajax_add')?&gt;";
      } else {
        url = "&lt;?php echo site_url('slider/ajax_update')?&gt;";
      }
  
      // ajax adding data to database
      $.ajax({
      url : url,
      type: "POST",
      data: $('#form').serialize(),
      dataType: "JSON",
      success: function(data)
      {
  
          if(data.status) //if success close modal and reload ajax table
          {
              $('#modal_form').modal('hide');
              reload_table();
          }
          else
          {
              for (var i = 0; i &lt; data.inputerror.length; i++) 
              {
                  $('[name="'+data.inputerror[i]+'"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
                  $('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]); //select span help-block class set text error string
              }
          }
          $('#btnSave').text('save'); //change button text
          $('#btnSave').attr('disabled',false); //set button enable 
  
  
      },
      error: function (jqXHR, textStatus, errorThrown)
      {
          alert('Error adding / update data'+errorThrown);
          $('#btnSave').text('save'); //change button text
          $('#btnSave').attr('disabled',false); //set button enable 
  
      }
      });
      }
  
  //end script
  
  &lt;div class="modal-body form"&gt;
                      &lt;form  id="form" class="form-horizontal" enctype="multipart/form-data" method="post" accept-charset="utf-8"&gt;
                      &lt;input type="hidden" value="" name="id"/&gt; 
                      &lt;div class="form-body"&gt;
  
                      &lt;div class="form-group"&gt;
                          &lt;label class="control-label col-md-3"&gt;Title&lt;/label&gt;
                          &lt;div class="col-md-9"&gt;
                              &lt;input name="title" placeholder="Caption" class="form-control" type="text"&gt;
                              &lt;span class="help-block"&gt;&lt;/span&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
  
                          &lt;div class="col-md-12"&gt;
                          &lt;input type="file" name="userfile" class="form-control"/&gt;
                              &lt;span class="help-block"&gt;&lt;/span&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
  
  
  
                  &lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>controller <strong>slider.php</strong></p>
  
  <pre><code> public function __construct()
  {
      parent::__construct();
      $this-&gt;load-&gt;model('slider_model','person');
  }
  
  public function ajax_add()
      {
  
  
      $this-&gt;_validate();
      $data = array(
              'title' =&gt; $this-&gt;input-&gt;post('title'),
              'image' =&gt; $this-&gt;input-&gt;post($this-&gt;upload()),
          );
      $insert = $this-&gt;person-&gt;save($data);
      echo json_encode(array("status" =&gt; TRUE));
  }
  
   public function upload()
  {
  $config['upload_path'] = "./images/";
  $config['allowed_types'] = 'jpg|jpeg|gif|png';
  $this-&gt;load-&gt;library('upload',$config);
  if(!$this-&gt;upload-&gt;do_upload()) {
      $error = array('error' =&gt;$this-&gt;upload-&gt;display_errors());
      echo $error;
  }
  else {
      $file_data = $this-&gt;upload-&gt;data();
      $filename = $file_data['file_name'];
      $this-&gt;main_m-&gt;save('title',$filename);
      $data['img'] = base_url().'/images/'.$file_data['file_name'];
      return $filename;
  }
  }
  </code></pre>
  
  <p>model <strong>slider_model.php</strong></p>
  
  <pre><code>public function save($data)
  {
      $this-&gt;db-&gt;insert($this-&gt;table, $data);
      return $this-&gt;db-&gt;insert_id();
  }
  </code></pre>
  
  <p>whats wrong with my code? that code works if I am not using ajax but fail if I am using ajax</p>