Ajax没有发送数据[Codeigniter]

i have a problem in ajax, indeed, i try to send value with ajax to my upload function before submit. But when i check the $_POST array in my php code, there is only the value of the form, and not from the ajax, and I don't know why. Here is my code :

HTML:

<button id="btn_saisie" class="btn btn-app saver adddocu" ><i class="fa fa-save whiter"></i></button>

<form action="/uploader/adddocu" id="form_saisie" class="form_saisie" method="POST" enctype="multipart/form-data">

<input type="file" name="document" class="val_peage form-control form_num" id="document" data-rest="document" placeholder="Document">
<input type="text" name="description" class="val_parking form-control form_num" id="description" data-rest="description" placeholder="Description">

JS :

$( ".adddocu" ).click(function() {
if ($('#document').val() != "" && $('#description').val() != ""){
    api_sendvalue_adddoc();
}
if ($('#document').val() == "")
    alert('test');
else if ($('#description').val() == "")
    alert('test2'); });


function api_sendvalue_adddoc(){
user = JSON.parse(sessionStorage.getItem('user'));
pays = localStorage.getItem("pays");
magasin = localStorage.getItem("magasin");
$.ajax({
    type: 'POST',
    url: '/uploader/adddocu',
    data: {pays:pays, magasin:magasin},
    success: function(data){
                alert(data);
                $("#form_saisie").submit();
      console.log(data);
    },
            error: function(xhr){
                alert(xhr.responseText);
                console.log(xhr.responseText);
            }
}); }

PHP:

public function adddocu(){
    $path = './asset/upload/pdf/';
    $path2 = '/asset/upload/pdf/';
    $config['upload_path']   = $path;
    $config['encrypt_name']   = false;
    $config['file_ext_tolower']  = true;
    $config['allowed_types']  = 'pdf';

    // die(var_dump($_POST));
    $this->load->library('upload', $config);
    foreach($_FILES as $id => $name)
    {
        $this->upload->do_upload('document');
        $upload_data = $this->upload->data();
        $url =  $path2 . $upload_data['file_name'];
        $data = array('nom' => $upload_data['raw_name'], 'description' => $_POST['description'], 'url' => $url, 'user_id' => '17');
        $this->db->insert('pdf', $data);
    }
    redirect("/login/docu");
}

So, when I var_dump the $_POST array, I only have the value of "description", and not of "pays" and "magasin".

Can you help me please?

Thanks for your time.

The issue is because you are not preventing the form from being submit normally, so the AJAX request is cancelled. Instead of using the click event of the button, hook to the submit event of the form and call preventDefault(). Try this:

$('#form_saisie').submit(function(e) {
    e.preventDefault();

    if ($('#document').val() != "" && $('#description').val() != ""){
        api_sendvalue_adddoc();
    }

    if ($('#document').val() == "")
        alert('test');
    else if ($('#description').val() == "")
        alert('test2'); 
});

Seems like you are accessing localstorage value , you are posting it somewhere and then submiting the form.

More you are submiting the form which dont have this pays & magasin so i have a trick using which you can achieve it.

Create two hidden inputs inside your HTML form like

<input type="hidden" name="pays" id="pays">
<input type="hidden" name="magasin" id="magasin">

Now before ajax call give them values after getting it from local storage, like this.

user = JSON.parse(sessionStorage.getItem('user'));
pays = localStorage.getItem("pays");
magasin = localStorage.getItem("magasin");

$("#pays").val(pays);
$("#magasin").val(magasin);

$.ajax({ .... });

Continue your code and enjoy. Hopefully it will work for you.

EDIT:

Here is a working example of a ajax post to codeigniter:

View

<script> 
    $( document ).ready(function () {
      // set an on click on the button
      $("#button").click(function () {
        $.ajax({
              type: 'POST',
              url: "[page]",
              data: {pays: "asd", magasin: "dsa"},
              success: function(data){
                          alert(data);
                          $("#text").html(data);
                       console.log(data);
              },
                      error: function(xhr){
                          alert(xhr.responseText);
                          console.log(xhr.responseText);
                      }
          });

      });
    });
  </script>

Controller

<?php
    // main ajax back end
    class Time extends CI_Controller {
        // just returns time
        public function index()
        {
            var_dump($_POST);
            echo time();
        }
    }
?>

Output

array(2) {
  ["pays"]=>
  string(3) "asd"
  ["magasin"]=>
  string(3) "dsa"
}
1473087963

Working example here

So you should check the request that you're making from AJAX, on dev console. There you should get the response with the var_dump($_POST).

to debug try to make your controller return only the $_POST data, comment the rest. and same thing on javascript side, test only the ajax post and data received.