title: upload image using codeigniter with ajax and formdata
body: <p>I like to upload image using codeigniter Framework in PHP with JQuery AJAX without redirecting the page.  When i upload the image it redirecting to controller area and the values are not insert to database , ajax response also not coming , </p>
  
  <p>can any one pls solve my issue.... Thanks in advance...</p>
  
  <p><strong>This is my view area.</strong></p>
  
  <pre><code>&lt;form  action="&lt;?php echo site_url("Admin_Creator/do_upload")?&gt;" enctype="multipart/form-data" accept-charset="utf-8" name="formname" id="frm_imageuupload"  method="post"&gt;
                 &lt;div class="form-group"&gt;
                 &lt;label for="menu"&gt;Select Menu&lt;/label&gt;
                     &lt;select class="form-control" id="selectmenuid"&gt;
                      &lt;option value=""&gt;-- Select Menu --&lt;/option&gt;
                      &lt;?php foreach($showData as $show):?&gt;
                        &lt;option value="&lt;?php echo $show-&gt;menu_id?&gt;"&gt;&lt;?php echo $show-&gt;menu_name?&gt;&lt;/option&gt;
                      &lt;?php endforeach;?&gt;
                      &lt;/select&gt;
                  &lt;/div&gt;
                 &lt;div class="form-group"&gt;
                 &lt;label for="menu"&gt;Select Sub Menu&lt;/label&gt;
                     &lt;select class="form-control" id="selectsubmenu"&gt;
                         &lt;option&gt;--Select Sub Menu--&lt;/option&gt;
                      &lt;/select&gt;
                 &lt;/div&gt;
                &lt;div class="form-group"&gt;
                    &lt;label for="imagetitle"&gt;Image Title&lt;/label&gt;
                    &lt;input type="text" class="form-control" name="imagetitle" id="imagetitle" placeholder="Enter Image Title" required="required"&gt;
                  &lt;/div&gt;
                 &lt;div class="control-group form-group"&gt;
                          &lt;div class="controls"&gt;
                              &lt;label&gt;Upload Photo:&lt;/label&gt;
                              &lt;input name="file" type="file" id="image_file" required&gt;
                              &lt;p class="help-block"&gt;&lt;/p&gt;
                          &lt;/div&gt;
                 &lt;/div&gt;
                 &lt;button type="submit" class="btn btn-primary" id="submit"&gt;Submit&lt;/button&gt;
             &lt;/form&gt;
  </code></pre>
  
  <p><strong>This is my Ajax coding</strong></p>
  
  <pre><code>$('#submit').on('click',function(){
          var inputFile=$('input[name=file]');
          var fileToUpload=inputFile[0].files[0];
          var other_data = $('#frm_imageuupload').serializeArray();
          var formdata=new FormData();
          formdata.append(fileToUpload);
          formdata.append(other_data);
           //now upload the file using ajax
           $.ajax({
               url:"&lt;?php echo base_url('Admin_Creator/do_upload') ?&gt;",
               type:"post",
               data:formdata,
               processData:false,
               contentType:false,
                success: function(data){
              if (data== 'true'){   
                 window.location.reload();
              }
              else{
                 alert("Pls Try Again");
              }
             }
           });
  </code></pre>
  
  <p><strong>This is my controller Coding...</strong></p>
  
  <pre><code> public function do_upload(){
          $config['upload_path']="./upload";
          $config['allowed_types']='gif|jpg|png';
          $this-&gt;load-&gt;library('upload',$config);
          if($this-&gt;upload-&gt;do_upload()){
          $data = array('upload_data' =&gt; $this-&gt;upload-&gt;data());
          $data1 = array(
          'menu_id' =&gt; $this-&gt;input-&gt;post('selectmenuid'),
          'submenu_id' =&gt; $this-&gt;input-&gt;post('selectsubmenu'),
          'imagetitle' =&gt; $this-&gt;input-&gt;post('imagetitle'),
          'imgpath' =&gt; $data['upload_data']['file_name']
          );  
          $result= $this-&gt;Admin_model-&gt;save_imagepath($data1);
          if ($result == TRUE) {
              echo "true";
          }
          }
  
       }
  </code></pre>
  
  <p><strong>This is my model coding</strong></p>
  
  <pre><code>public function save_imagepath($data1)
      {
          $this-&gt;db-&gt;insert('images', $data1);
           return $this-&gt;db-&gt;insert_id();
      }
  </code></pre>