0

I am trying to pass multiple parameters but dont know the syntax..PLz help

function formSubmit() {

    var name = document.getElementById('name').value;
    var email = document.getElementById('email').value;
    var phone = document.getElementById('phone').value;
    var message = document.getElementById('message').value;

    // This is where the problem is:
    var params = "name="+name&"email="+email&"phone="+phone&"message="+message;

    var xhr = new XMLHttpRequest;
    xhr.open('POST' , 'ajax.php', true);
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')

    xhr.onload = function() {
        if(this.status ==200) {
            console.log(this.responseText);
        }
    }
    xhr.send(params);
}
AamirR
  • 9,385
  • 3
  • 47
  • 59
Navish
  • 81
  • 2
  • 11
  • 1
    Welcome to SO!, seems there are several problems with your code, e.g. XMLHttpRequest should be `XMLHttpRequest()`, check this out https://stackoverflow.com/a/9713078/1244597 – AamirR Jul 15 '18 at 10:56
  • 1
    name="+name&"email="+email&"phone="+phone&"message="+message; this line has a problem the "&" sign must be inside in the double quote – bdalina Jul 15 '18 at 11:01
  • var params = "name="+name+"&email="+email+"&phone="+phone+"&message="+message; – bdalina Jul 15 '18 at 11:02

1 Answers1

0

I see correct coments from bdalina and AamirR. But i didint see answer uder your question. So I make one used good coments from (bdalina and AamirR) for others who problem you and I have.

function formSubmit() {
var name = document.getElementById('name').value;
var email = document.getElementById('email').value;
var phone = document.getElementById('phone').value;
var message = document.getElementById('message').value;

// This is where the problem is:
var params = "name="+name"&email="+email"&phone="+phone"&message="+message;
//for send more parameters them one use "$" in the next POST identifier.
// like "nameOfValue="+value1"&NameNextValue"+value2

var xhr = new XMLHttpRequest();
//XMLHttpRequest is a function so u must have () on end of function name. XMLHttpRequest()
xhr.open('POST' , 'ajax.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')

xhr.onload = function() {
    if(this.status ==200) {
        console.log(this.responseText);
    }
}
xhr.send(params);}
SnaYQ
  • 16
  • 2