Pass jQuery variable inside Ajax

Asked

Viewed 923 times

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 the 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 in ajax/import_list:

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

How can I pass the id_cliente inside ajax? If I pass on as @Kayobruno’s response, I need to go over with var?

2 answers

2


If you want to pass the form all put data: form, as it already has a global variable.

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

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

You have to place the Form Element inside the 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">
  • It’s supposed to work. It’s not carrying anything ? You checked if the Hidden field is worth anything ?

  • The Hidden field has value, and is carrying nothing.. If I put date: form, it takes only the upload data.. is it because of the function above jquery?

  • Before you call Ajax, do it console.log($('form').serialize()); and see what returns.

  • Returned only id_client=3

  • Same thing... I can’t receive the data from the id_client, and that way, neither the upload data, can it be because of the form? i put so <form id="guests">...

  • Good observation, I had not come across. It has mandatory parameters, as the enctype.

  • I’m out of luck today, gave the same thing rssrsrsrsr...

  • In PHP do print '<pre>'; print_r($_POST) print '</pre>';

  • I’ve done, POST, GET, REQUEST AND FILES all return array()

  • Just leave it at AJAX: data: $('form').serialize(),

  • In an attempt, I did as follows @Gumball: inside the ajax... data: {id_client: id_client}, ai with print_r($_REQUEST) or POST, appears. Beyond the date: form, I added the data:..

Show 7 more comments

1

You can pass as many parameters as you want 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);    
    }
});

});

  • But I need to point out the var?

  • didn’t work out at all...

  • 1

    Yes, you need to declare the variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.