尝试使用AJAX从PHP中检索值

时间:2013-05-27 12:13:00

标签: php javascript html ajax

这就是我想要的。我试图调用一个函数试验,从PHP中检索值为1到29的值,并在名为T1,T2 ... T29的文本输入框中显示结果。

function calculate() {
    for (var i = 1; i < 30; i++) {
        trial(i);
    }
}

function trial(i) {
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById('T' + i).value = xmlhttp.responseText;
        }
    }


    xmlhttp.open("GET", "MANAGER/manager.php?rownum=" + i, true);
    xmlhttp.send();

    return;
}

它不起作用。你能建议一个解决方案吗?

1 个答案:

答案 0 :(得分:0)

问题在于,您要全局声明变量xmlhttp,因此您将覆盖每次迭代的回调和所有内容。使用var关键字将其设为本地。

相关问题