Passing variable in jQuery within Ajax

3

I have the following Form:

<div class="row" id="importar">
    <form id="convidados">
        <input type="hidden" value="<?php echo $this->uri->segment(4); ?>" id="id_cliente" name="id_cliente">
        <div id="multiple_upload">
            <input type="file" id="uploadChange" onchange="checkfile(this);" />
            <div id="message">Selecione o Arquivo</div>
            <input type="button" id="botao_" class="btn btn-primary" value="Importar" disabled />
            <div id="lista">

            </div>
        </div>
    </form>
<div>

And jQuery:

// importar 
$(function () {

    var form;
    $('#uploadChange').change(function (event) {
        form = new FormData();
        form.append('uploadChange', event.target.files[0]); // para apenas 1 arquivo
        //var name = event.target.files[0].content.name; // para capturar o nome do arquivo com sua extenção
    });

    $('#botao_').click(function () {
        var id_cliente = $("#id_cliente").val();

        $.ajax({
            url: basePath + 'ajax/importar_lista',
            data: {upload: form, id_cliente: id_cliente},
            processData: false,
            contentType: false,
            type: 'POST',
            success: function (data) {
                console.log("success");     
                $('#importar').html("<div style='padding: 100px;'><center><img src='../../../assets/img/importando-lista.gif'></center></div>");
                setTimeout(function(){
                    $('#importar').html(data);
                },3000);    
            }
        });
    });
}); 

I need to add the following variable to pass on in ajax / import_list:

var id_cliente = $("#id_cliente").val();

How can I pass id_cliente inside ajax? If I go over as the answer from @KayoBruno, I need to go over with var?

    
asked by anonymous 23.12.2016 / 15:03

2 answers

2

If you want to pass form everything puts data: form , since you already have a global variable.

Another thing, in its new FormData it has to be like this:

form = new FormData($('form')[0]); 

You have to put the Form Element within FormData .

$('#botao_').click(function () {
    var id_cliente = $("#id_cliente").val();

    $.ajax({
        url: basePath + 'ajax/importar_lista',
        data: form,
        processData: false,
        contentType: false,
        type: 'POST',
        success: function (data) {
            console.log("success");     
            $('#importar').html("<div style='padding: 100px;'><center><img src='../../../assets/img/importando-lista.gif'></center></div>");
            setTimeout(function(){
                $('#importar').html(data);
            },3000);    
        }
    });
});

And in normal PHP:

$idCliente = $_POST['id_cliente'];

HTML

<form id="convidados" method="POST" enctype="multipart/form-data">
    
23.12.2016 / 16:32
1

You can pass as many parameters as you like on the date.

$('#botao_').click(function () {
  $.ajax({
    url: basePath + 'ajax/importar_lista',
    data: {form: form, id_cliente: id_cliente},
    processData: false,
    contentType: false,
    type: 'POST',
    success: function (data) {
        console.log("success");     
        $('#importar').html("<div style='padding: 100px;'><center><img src='../../../assets/img/importando-lista.gif'></center></div>");
        setTimeout(function(){
            $('#importar').html(data);
        },3000);    
    }
});

});

    
23.12.2016 / 15:20