Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax send no data [ 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.

like image 214
Corentin Verpillat Avatar asked Jul 09 '26 10:07

Corentin Verpillat


1 Answers

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.

like image 88
Punit Gajjar Avatar answered Jul 12 '26 01:07

Punit Gajjar