-2

In Codeigniter by using ajax i am adding record to (products) and everything is working fine so i decided to add (image) field , and for some reason it's no longer adding any record to database

and i add input type=file to my form

   <input type="file" name="image">

and i add this to my controller

   $image =    $this->input->post('image');


$data = array('title'=>$title,'description'=>$description,'price'=>$price,'quantity'=>$quantity,'image'=>$image);

but when i remove $image = $this->input->post('image'); it gets to work again

just in case this is my ajax code

  var postData = $(this).serialize();

$.ajax({
type:'post',
url: baseURL+"admin/Products/add_product",
data:postData,
dataType:'json',
  success:function(data){



} 


});

any ideas how to solve it?

miken32
  • 42,008
  • 16
  • 111
  • 154
Mohamad Alasly
  • 330
  • 3
  • 17
  • First, I would make sure your form tag has `enctype="multipart/form-data"` Aside from that, check the binds for your insert script. Any errors being generated? – Michael Jul 15 '18 at 23:37

2 Answers2

1

Hope this will help you :

Your ajax should be like this :

var formdata = new FormData();
$.ajax({
    type: 'POST',
    url: baseURL+"admin/Products/add_product",
    data: formdata,
    cache: false,
    contentType: false,
    processData: false, 
    success: function(response)
    {
       alert(response);   
    }
});

In controller accessing image using $_FILES super variable

public function add_product()
{
   print_r($_FILES);
   print_r($this->input->post());
   exit;
}

Note : make sure URL path is correct , see ur network tab to see the output

For more : https://www.formget.com/ajax-image-upload-php/

Pradeep
  • 9,667
  • 13
  • 27
  • 34
0

May Be You can use instead of baseURL

var formdata = new FormData();

    $.ajax({
        type: 'POST',
        url: <?=base_url()?>+"admin/Products/add_product",
        data: formdata,
        cache: false,
        contentType: false,
        processData: false, 
        success: function(response)
        {
           alert(response);   
        }
    });
Shafeer khan
  • 464
  • 5
  • 8