javascript - 在同一个 Web html 页面中获取结果

标签 javascript php html ajax

我已经没有办法执行我的 html/php 网页了。

场景如下:

我从正文部分的 html 网页加载了一个 php 脚本,该脚本从 mysql 数据库中检索数据,将其呈现在表中,然后将其返回到初始网页的部分。 当我得到这张表时,在 Id(第一列)上有一个链接,假设通过单击它可以在同一页面的另一个部分中提供信息,但它没有。 它在另一个网页中显示信息。

这是主要的 html 代码:

<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://www.w3schools.com/w3css/w3.css">
<head>
<style>
#header {
    background-color:black;
    color:white;
    text-align:center;
    padding:5px;
}
#section {
    width:100%;
    float:left;
    padding:5px;         
}
#footer {
    background-color:black;
    color:white;
    clear:both;
    text-align:center;
    padding:5px;    
    text-shadow: 1px 1px;   
}
</style>
<script>
function getStudentStatus(Id) {
    if (Id == "") {
        document.getElementById("StuStat").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("StuStat").innerHTML = xmlhttp.responseText;
            }
        }
        console.log("looking for :"+Id);
        xmlhttp.open("GET","getStudentStatus.php?StudentId="+Id,true);
        xmlhttp.send();
    }
}
function getStudentInfo(Id) {
    if (Id == "") {
        document.getElementById("StuInfo").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("StuInfo").innerHTML = xmlhttp.responseText;
            }
        }
        console.log("looking for :"+Id);
        xmlhttp.open("GET","getStuInfo.php?StudentId="+Id,true);
        xmlhttp.send();
    }
}

var intShowResult = setInterval(function() {getStudentStatus("q") },60000);

</script>
</head>
<body onload="getStudentStatus('q')">

<div class="w3-container w3-orange">
  <h1>Student Status</h1> 
  <h4>test bed</h4>
</div>
<br><br><br>

<div id="section">
<p>Result: <span id="StuStat"></span></p>
</div>

<div id="footer">
Copyright © xxxxxx.com
</div>



<div id="StuInfo">
<p>Cycle Info of:<span id="StuInfo"></span></p>
</div>


</body>
</html>

有什么想法吗? /库尔

最佳答案

尝试从 body 标签中删除 onload="getStudentStatus('q')",注释掉 var intShowResult = setInterval(function() {getStudentStatus("q") },60000 ); 并尝试在末尾将其添加到您的 javascript 中。

第一次调用 getStudentStatus 发生在页面加载完成之前,因此如果它执行成功,则 html 中没有容器元素来接受响应。在调用该函数之前确保页面内容已完全加载。

function initialise(){
    call getStudentStatus(this,'q');
    setInterval( function() { getStudentStatus('q') }, 60000 );
}

document.addEventListener('DOMContentLoaded',initialise,false);

关于javascript - 在同一个 Web html 页面中获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32537822/

相关文章:

javascript - 如何使用 Laravel 发送跨域 Ajax POST

javascript - 在 javascript 中允许使用字符、数字和 - 的正则表达式

php - 图像复制并不总是有效

html - 在 <li> 和 <ul> 中使用类

html - 最后 2 个元素的悬停背景颜色不变

javascript - 寻找在不创建其他 div 的情况下以每 100 速率随机显示 div 的解决方案

javascript - Angular - 如何跨模块共享 Controller ?

php - 函数外的变量中不允许有表达式

javascript - 通过jquery从文本框中获取值

javascript - VueJS : Bind values to Component