codeigniter form upload with images using ajax

user3840485

I need to submit image with form data,I have search a better method for this, but unfortunately I couldn't find a better way for my purpose, previously done with this.serialize() method. But it doesn't work with images

here is my code

view

 <?php
          $this->load->helper('form');
          $attributes =  array('method'=>'post','name'=>'create_company','id'=>'create_company');
          echo form_open_multipart('',$attributes);?>
    <label>Code : </label> <?php echo form_input('code');?><br/><br/>
    <label>Name : </label> <?php echo form_input('name');?><br/><br/>
    <label>Logo : </label><input type="file" name="userfile"/><br/><br/>
    <label>URL : </label> <?php echo form_input('url');?><br/><br/>
    <label>Description : </label> <textarea name="description" rows="4" cols="50"> </textarea><br/><br/>
    <input type="submit" name="submit" value="Save"/>
    <?php echo form_close(); ?>     
</div>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>//no need to specify the language
       $(document).ready(function() {

       $('#create_company').on("submit",function(e) {

            e.preventDefault();

            $.ajax({
                type: "POST",
                url: "<?php echo site_url('site/upload'); ?>",
                data: $(this).serialize(),
                success: function(data){
                    var site_url = "<?php echo site_url('site/create_branch_form'); ?>";
                    var json = $.parseJSON(data);
                    site_url = site_url +"/" + json.results[0].id ;
                    alert(site_url);
                    $("#content").load(site_url);
                    alert(data);
                }
           });            
        });
      });
    </script>

controller

public function upload(){

        //insert company details
    $this->load->model('company_model');
    $data['results'] = $this->company_model->insert_company();
    $this->output->set_output(json_encode($data));                
    }

model

function insert_company(){
        //user details
        $username =  $this->session->userdata('username');
        //$query = $this->db->get_where('userdetails', array('username' => $username));

        $query =  $this->db->get_where('userdetails',array('username'=>$username));

        foreach ($query->result() as $function_info) 
        {
            $this->userid = $function_info->id;
        }

    $new_company_insert_data = array(

        'code' => $this->input->post('code'),
                    'name' => $this->input->post('name'),
                    'logo' => $imgpath['file_name'],
                    'url' => $this->input->post('url'),
                    'description' => $this->input->post('description'),
                    'createdat' => date('Y-m-d H:i:s',strtotime('')),
                    'status' => 0,
                    'userid' => $this->userid
             ); 

        $this->db->insert('companydetails',$new_company_insert_data);
        //get id of the last inserted row
        $this->load->database();
        $query = $this->db->query("SELECT MAX(id) AS id FROM companydetails");
        return $query->result();
}
blackbishop

Try with FormData object. You should have something like this in your JS code :

 $(document).ready(function() {

   $('#create_company').on("submit",function(e) {

        e.preventDefault();
        var formData = new FormData(this);

        $.ajax({
            type: "POST",
            url: "<?php echo site_url('site/upload'); ?>",
            data: formData,
            cache: false,
            contentType: false,
            processData: false
            success: function(data){
                //function success
            },
            error: function(data){
            //error function
        }
       });            
    });
  });

Hope this could help.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Upload form with image in codeigniter using ajax

From Dev

CodeIgniter, Jquery Ajax, Form Submission and File Upload

From Dev

How to upload multiple images using codeigniter?

From Dev

Upload files Using AJAX & CodeIgniter 3.0.1

From Dev

unable to upload file in codeigniter using AJAX

From Dev

Can you upload images using Ajax?

From Dev

Upload multiple images with codeigniter

From Dev

Updating list using AJAX in Codeigniter (within a form)

From Dev

Upload an image with ajax without using a form

From Dev

Upload image using ajax and form submitting

From Dev

File upload using Jquery ajax without form

From Dev

Upload image using ajax and form submitting

From Dev

codeigniter form with image upload

From Dev

Images upload form laravel

From Dev

CodeIgniter ajax upload image (ajax works, not CodeIgniter)

From Dev

Laravel: Upload form data with images in a foreach loop using Intervention

From Dev

How do I upload multiple images and text file in a single form with the different button in Codeigniter?

From Dev

ajax upload file form

From Dev

Registration form with image upload in Codeigniter

From Dev

codeigniter dropzone ajax upload with csrf

From Dev

codeigniter jquery ajax file upload

From Dev

Passing form data to controller using AJAX and jquery with Codeigniter

From Dev

codeigniter submit form with text and images

From Dev

Form submit to upload file and also other fields using ajax

From Dev

upload a file in the same page as the form using php and ajax not working

From Dev

Upload 2 images in an HTML form

From Dev

upload images using RESTAPI

From Dev

How to upload images via Ajax?

From Dev

How to upload 2 separate images in codeigniter

Related Related

HotTag

Archive