Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Ajax更新页面上的数据,该页面使用PHP从文本文件读取_Javascript_Php_Ajax - Fatal编程技术网

Javascript 使用Ajax更新页面上的数据,该页面使用PHP从文本文件读取

Javascript 使用Ajax更新页面上的数据,该页面使用PHP从文本文件读取,javascript,php,ajax,Javascript,Php,Ajax,我想使用Ajax更新网页上的数据。使用PHP脚本读取此数据 我希望这个功能每5秒执行一次。出于某种原因,这根本没有发生 我的JavaScript: <script> function refresh(){ var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 20

我想使用
Ajax
更新网页上的数据。使用
PHP
脚本读取此数据 我希望这个
功能每5秒执行一次。出于某种原因,这根本没有发生

我的JavaScript:

<script>
 function refresh(){
   var xmlhttp = new XMLHttpRequest();
   xmlhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
     document.getElementClassName("scroll").innerHTML = this.responseText;
   }
 };
 xmlhttp.open("GET", "gm.php?q=" + "<?php echo $mf[0]."-".$mf[1].".txt" ?>", true);
// The PHP variables refer to a specific file
 xmlhttp.send();
 }
 setInterval(refresh,5000);
</script>
<script>
 function refresh(){
   var xmlhttp = new XMLHttpRequest();
   xmlhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
     document.getElementsByClassName("scroll")[1].innerHTML = this.responseText;
   }
 };
 xmlhttp.open("GET", "gm.php?q=" + '<?php echo $mf[0]."-".$mf[1].".txt" ?>',  true);
// The PHP variables refer to a specific file
 xmlhttp.send();
 }
 setInterval(refresh,5000);
</script>

我发现我的元素选择器是错误的,因为我使用的是
getelementclassname
而不是
getElementsByClassName
。另外,我没有在类中给出元素索引,因此无法选择元素

另一个原因是我在包含PHP脚本的JavaScript字符串中使用了引号

我的JavaScript:

<script>
 function refresh(){
   var xmlhttp = new XMLHttpRequest();
   xmlhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
     document.getElementClassName("scroll").innerHTML = this.responseText;
   }
 };
 xmlhttp.open("GET", "gm.php?q=" + "<?php echo $mf[0]."-".$mf[1].".txt" ?>", true);
// The PHP variables refer to a specific file
 xmlhttp.send();
 }
 setInterval(refresh,5000);
</script>
<script>
 function refresh(){
   var xmlhttp = new XMLHttpRequest();
   xmlhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
     document.getElementsByClassName("scroll")[1].innerHTML = this.responseText;
   }
 };
 xmlhttp.open("GET", "gm.php?q=" + '<?php echo $mf[0]."-".$mf[1].".txt" ?>',  true);
// The PHP variables refer to a specific file
 xmlhttp.send();
 }
 setInterval(refresh,5000);
</script>

document.getElementsClassName
NOT
document.getElementClassName
检查url参数Quotes我已将其更正为
document.getElementsByClassName(“滚动”)
,仍然没有帮助请打开浏览器中的控制台读取错误,然后可以修复它。该函数每5秒运行一次,这不是问题所在。刷新函数中有一些错误。查看浏览器控制台以测试这一点。可能是CORS错误或传递无效参数