0

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!');
}
Karlo Kokkak
  • 3,674
  • 4
  • 18
  • 33
Mitca Vicentiu
  • 196
  • 1
  • 14
  • Test the $_POST keys before you access them. Use something like `array_key_exists()` in your php to avoid the undefined index errors, and so you can handle errors or fill in defaults – WillardSolutions May 22 '18 at 14:33
  • I'm assuming correctly that the request is sent as JSON, yes? Then PHP won't automatically populate the `$_POST` array, since PHP can't/won't implicitly parse JSON request bodies. – deceze May 22 '18 at 14:35
  • What can i do in this situation? I don't really understand what's going on – Mitca Vicentiu May 22 '18 at 14:39
  • Read the duplicates at the top. – deceze May 22 '18 at 14:45
  • I read it, but i don't understand what's happening or what should i do -.- – Mitca Vicentiu May 22 '18 at 14:54
  • do you guys even read the docs ? there is a method to avoid exactly your problem in CI - just take `$this->input->post('key')` and everything is fine.... https://www.codeigniter.com/user_guide/libraries/input.html#using-post-et-cookie-or-server-data - and if you dont have any data at all look @thos two linked questions... – Atural May 22 '18 at 16:42
  • send your view file –  May 23 '18 at 04:15

0 Answers0