简体   繁体   English

尝试使用AJAX从PHP检索值

[英]Trying to Retrieve values from PHP using AJAX

This is what I am trying. 这就是我正在尝试的。 I am trying to call a function trial with retrieves a value from a PHP for values 1 to 29 and display the result in text input boxes named T1, T2...T29. 我正在尝试调用函数试用版,以从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;
}

It is not working. 它不起作用。 Could you please suggest a solution? 您能否提出解决方案?

The issue is that you are declaring the variable xmlhttp globally, so you are overwriting the callbacks and everything on each iteration. 问题是您要全局声明变量xmlhttp ,因此您将覆盖回调以及每次迭代中的所有内容。 Use the var keyword to make it local. 使用var关键字使其本地化。

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

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