Question :
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?
Answer :
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
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) {
// (...)
}
});