user2748436
user2748436

Reputation: 17

How can I send more than one variable to a php file using ajax and a get request?

I am trying to send an I.D. value and a name value to a php file using ajax. I can send just the I.D. variable just fine but when I try to add the name variable, the function stops working. How can I send both?

This works:

function click() {
    var name = clickedelement.getElementsByTagName('input')[0].value;
    var id = clickedelement.getElementsByTagName('input')[1].value;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("popupBox").innerHTML = xmlhttp.responseText;
        }
    }

    xmlhttp.open("GET", "friends2.php?id="+id, true);
    xmlhttp.send();
};

But when I try to add the name variable, it dosnt work.

function click() {
    var name = clickedelement.getElementsByTagName('input')[0].value;
    var id = clickedelement.getElementsByTagName('input')[1].value;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("popupBox").innerHTML = xmlhttp.responseText;
        }
    }

    xmlhttp.open("GET", "friends2.php?id="+id"&name="+name, true);
    xmlhttp.send();
};

Upvotes: 0

Views: 1192

Answers (2)

Jay Harris
Jay Harris

Reputation: 4271

     "friends2.php?id="+id+"&name="
// missing plus sign here ^

Upvotes: 2

sainiuc
sainiuc

Reputation: 1697

change to "friends2.php?id="+id+"&name="+name you just have a missing +

Upvotes: 2

Related Questions