javascriptphpajax

xhr.send(params) - multiple parameters


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);
}

Solution

  • I see correct comments from bdalina and AamirR. But i didn't see answer under your question. So I make one used good comments 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);}