How to pass value from javascript to AJAX

3

I have a javascript function that has AJAX code inside it. I want to pass the values from javascript 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;
}

Is it possible to do this, what am I missing?

    
asked by anonymous 02.07.2015 / 20:48

2 answers

6

The problem is that with

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

You get an element of the DOM. I believe you want to send the value or the text, if this is the case, do

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

returning string

    
02.07.2015 / 21:11
4

It is possible and should be done.

Using the AJAX date element or by going directly to 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) {
        // (...)
    }
});
    
02.07.2015 / 21:08