How to pass javascript value to AJAX

Asked

Viewed 512 times

3

I have a javascript function that has an AJAX code inside it. I want to pass the javascript values to AJAX, follow the code:

    function validarCamposComprar() {

    var campoNomeEmpresa = document.getElementById('fTxtCadastroNomeEmpresa');
    var campoNomeAdmin = document.getElementById('fTxtCadastroNomeAdmin');

    $.ajax({
        type: "POST",
        url: "email.php",
        data: { meuParametro1: campoNomeEmpresa, meuParametro2: campoNomeAdmin },
        complete: function (data) {
            // (...)
        }
    });

    return true;
}

It’s possible to do this, which I’m missing?

2 answers

6


The problem is that with

var campoNomeEmpresa = document.getElementById('fTxtCadastroNomeEmpresa');

you get an element from DOM. I believe you want to send the value or text, if that’s the case, do

var nomeEmpresa = document.getElementById('fTxtCadastroNomeEmpresa').value;
var nomeAdmin = document.getElementById('fTxtCadastroNomeAdmin').value;

returning string

  • This ai @paulojean, I tidied up in my reply, I had not noticed it.

4

It is possible and must be done.

Using the AJAX date element or directly passing the URL.

I usually do it this way:

var campoNomeEmpresa = $('campo1').value;
var campoNomeAdmin= $('campo2').value;
$.ajax({
    type: "POST",
    url: "email.php?meuParametro1=" + campoNomeEmpresa + "&meuParametro2=" + campoNomeAdmin,
    complete: function (data) {
        // (...)
    }
});

Browser other questions tagged

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