javascript - 为什么这两个参数获取相同的值?

标签 javascript php mysql ajax

我正在学习使用 PHP 作为后端的 Ajax。我正在尝试使用 JavaScript 中的 setTimeout 从数据库中获取和更新表值。

这是我的代码: (我已经在下面解释了这个问题)

A. ex1.php

<?php
$a=mysqli_connect("localhost", "root", "", "ndb");
$query="SELECT name from tab3";
$queryrun=mysqli_query($a, $query);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="jquery-2.2.0.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script> 
var update= setTimeout(myFunc2, 4000);
function myFunc2(){
var yhttp= new XMLHttpRequest();
    yhttp.onreadystatechange=function(){
        //if(yhttp.readyState==4 && yhttp.status==200)
        };
        yhttp.open("GET", "exresponse2.php", true);
        yhttp.send();
    }
</script>
<script>
var cmp= setTimeout(cmpFunc, 100);
function cmpFunc(){
    var h="";
    var h2="";
    var cmp2= setTimeout(cmp2Func, 2000);
    function cmp2Func(){
        h= '<?php $height=mysqli_num_rows($queryrun); echo $height; ?>';
        }
    var cmp3 =setTimeout(cmp3Func, 8000);
    function cmp3Func(){
        h2= '<?php $height2=mysqli_num_rows($queryrun); echo $height2; ?>';
        if(h==h2)
            {
                alert(h+" "+h2);
            }
            else
            {
                alert("Not same");
            }
        }
    }
</script>
</head>
<body>
<div id="id1" style="float: left; width: 300px">
The names are displayed below:</div>
<div id="id2" style="float: left; width: 200px">
<button onclick="myFunc2()">Submit</button>
</body>
</html>

B. exresponse.php

<?php
$a=mysqli_connect("localhost", "root", "", "ndb");
$query="SELECT name from tab3";
$queryrun=mysqli_query($a, $query);
$names=array();
while($row=mysqli_fetch_assoc($queryrun))
{
    $names[]= $row["name"];
}
$x="";
$count=0;
for($x=0; $x<25; $x++)
{
echo "Name: ".$names[$x];
echo "<br>";
}
?>
<html>
<head>
<script src="jquery-2.2.0.js"></script>
</head>
<title></title>
<body>
</body>
</html>

如您所见,当在浏览器上打开页面ex1.php时,100微秒后cmpFunc执行,然后2秒后cmp2Func执行Strong> 执行,变量 h 被分配一些值;再过 2 秒后,myFunc2 执行(更新数据库中的表)。之后,cmp3Func 执行,并为变量 h2 赋值。

但每次,只有 if 语句执行。但实际上表应该在 hh2 分配值之间更新,并且它们应该具有不同的值。我在这里做错了什么吗?

最佳答案

h 和 h2 始终相同的原因是,当 ex1.php 首次呈现给客户端时,javascript 的值在该时刻对 h 和 h2 进行了硬编码,因此每次您使用它们时它们都是相同的稍后调用该 js 函数。

我认为解决方案是将 $query="SELECT name from tab3"; 的 sql block 移动到另一个服务器页面,然后在 cmp3Func 中使用 ajax 调用来访问该页面 函数。

关于javascript - 为什么这两个参数获取相同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36116062/

相关文章:

javascript - Angular:动态呈现时 SVG 路径标记不可见

php - 获取 e_date 中 90 天记录之前的用户数

javascript - 将 PHP Post API 脚本转换为 jQuery API 调用

Ubuntu 中的 MySQL 连接

java - 类似的问题

mysql - 为什么SQL中没有 "first greater/less than [or equal to]"比较运算符?

javascript - d3 - 画线 n 次

javascript - 如何触发onclick?

javascript - Opera 用户 JS 中的 jQuery 不起作用

javascript - 将 Google Analytics 脚本粘贴到我的网站(head.php、header.php 或 page-header.php)?