繁体   English   中英

如何使用ajax和get请求将多个变量发送到php文件?

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

我正在尝试使用ajax将ID值和名称值发送到php文件。 我可以只发送ID变量,但是当我尝试添加name变量时,该函数停止工作。 如何同时发送?

这有效:

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

但是,当我尝试添加名称变量时,它没有起作用。

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

更改为“ friends2.php?id =“ + id +”&name =“ +名称,您只是缺少了+

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM