title: Codeigniter: Uploading image through Ajax and storing in db
body: <p>I am using CI 3.0.1 was uploading and inserting image to db successfully before i used ajax, i guess trying to do it with ajax i'm missing something which isn't even sending data to upload maybe because we have to use <strong>multipart()</strong> in form while in ajax we are just sending data direct to controller, another thing i don't know how to receive the variable in response <br>
  my Ajax request function is:<br></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
   $(document).ready(function() {
   alert("thirddddddddd");
          $('#btnsubmit').click(function() 
          {
              alert("i got submitted");
              event.preventDefault();
               var userfile = $("input#pfile").val();
                          alert("uploading");
      $.ajax({
          url: '&lt;?php echo base_url(); ?&gt;upload/do_upload', //how to receive var here ?
              type: 'POST',
               cache: false,
               data: {userfile: userfile},
              success: function(data) {
              alert(data);
              $('.img_pre').attr('src', "&lt;?php echo base_url().'uploads/' ?&gt;");
                  $('.dltbtn').hide();
              },
              error: function(data)
              {
              console.log("error");
              console.log(data);
                 alert("Error :"+data);
              }
          });
      });
  });
  </code></pre>
  
  <p>
  And my controller Upload's function <strong>do_upload</strong> is:</p>
  
  <pre><code> public function do_upload()
      {
  
              $config['upload_path']          = './uploads/'; #$this-&gt;config-&gt;item('base_url').
              $config['allowed_types']        = 'gif|jpg|png|jpeg';
  
              $this-&gt;upload-&gt;initialize($config);
  
              if ( ! $this-&gt;upload-&gt;do_upload('userfile'))
              {
                      $error = array('error' =&gt; $this-&gt;upload-&gt;display_errors());
  
                      $this-&gt;load-&gt;view('layouts/header');
                      $this-&gt;load-&gt;view('home_page', $error);
                      $this-&gt;load-&gt;view('layouts/footer');
              }
              else
              {
                      $data = array('upload_data' =&gt; $this-&gt;upload-&gt;data());
                      $imagedata = $this-&gt;input-&gt;post('uerfile');
  
                      $session_data = $this-&gt;session-&gt;userdata('logged_in');
                      $id =  $session_data['id'];
  
  
                      $result = $this-&gt;model_edit-&gt;update_dp($id, $imagedata);
  
                      $image_name = $result-&gt;images;
  
                      $this-&gt;session-&gt;set_userdata('image', $image_name);
  
                      echo json_encode($name = array('image' =&gt; $image_name));
  
                      // $image_name = $this-&gt;upload-&gt;data('file_name');
                      // $data = array('upload_data' =&gt; $this-&gt;upload-&gt;data());
  
                      // redirect("account");
              }
      }
  </code></pre>
  
  <p>Now image is not even going to <strong>uploads</strong> folder, if any other file is needed tell me i'll post it here. Thanks</p>