I have an ajax
.Every time the ajax
it's called i get Undefined index error for every data
element from ajax
and at the end i get another error: Cannot modify header information - headers already sent by
.
In network field from inspector i can see two situation.After submit, it will be only one request to the server with file = edit_product(this will have status code 200) or there will be 2 requests: one with status code 200 and one without status code at all.The one with status code 200 will be caused by document and the one without status code will be caused by xhr.
In case 2(when there are 2 requests) the function will work.
In case 1(when there is only 1 request) the function will not work.
It may be a duplicate, but i don't think so.I searched a lot on stackoverflow and other forums...
ajax:
$.ajax({
method:"POST",
url: "<?php echo base_url('products/edit_product'); ?>",
dataType: "json",
data: {
prd_id: id,
prd_name: name,
prd_cat: category,
prd_ing: ingredients,
prd_grams: grams,
prd_price: price,
prd_show: show,
prd_img: img
},
success:function(resp){
if(resp.done){
console.log('its working');
}
},
error: function(xhr, ajaxOptions, thrownError){
console.log('eroare ' + thrownError);
}
});
Controller's function:
public function edit_product(){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$id = $_POST["prd_id"];
$test = array(
"product_name" => $_POST['prd_name'],
'category' => $_POST['prd_cat'],
'ingredients' => $_POST['prd_ing'],
'grams' => $_POST['prd_grams'],
'price' => $_POST['prd_price'],
'show' => $_POST['prd_show'],
'image' => $_POST['prd_img'],
);
echo json_encode(array(
'done' => true,
));
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/quartiere/assets/images';
$config['allowed_types'] = 'jpg|png|jpeg|JPG|JPEG';
$config['max_size'] = '0';
$config['max_width'] = '10000';
$config['max_height'] = '10000';
$this->load->library('upload',$config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$food_image = 'noimage.jpg';
}else{
$data = array('upload_data' => $this->upload->data());
$food_image = $_FILES['userfile']['name'];
}
$this->food_model->edit_product($id,$test);
$this->session->set_flashdata('product_edited','Ai editat produsul cu success!');
}