如何问javascript等待mysql为php变量赋值? [英] How to ask javascript wait for mysql assign value to php variable?

查看:62
本文介绍了如何问javascript等待mysql为php变量赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php
  $query3 = "SELECT message FROM messageslive LIMIT 1";
  $result3 = mysql_query($query3,$connection) or die (mysql_error());
  confirm_query($result3);
  while($userinfo3 = mysql_fetch_array($result3)){
      $msgLive = $userinfo3['message'];
  }
?>

<script type="text/javascript">
  var msg = "<?php echo $msgLive ; ?>";
</script>

我担心的是当数据库表有太多数据要搜索和检索时,所以PHP变量 $ msgLive 还没有值,所以Javascript变量 var msg 获取空值。如何询问javascript等到PHP变量 $ msgLive 得到的值然后只将值从php变量传递给Javascript?

What I worry is when the database table has too many data to search and retrieve, so PHP variable $msgLive doesn't have value yet, so Javascript variable var msg get empty value. How to ask javascript wait until PHP variable $msgLive got the value then only transfer the value from php variable to Javascript?

推荐答案

你不应该担心这个,因为首先服务器脚本执行,花时间与数据库通信,分配 msgLive 变量值,最后生成发送给客户端的HTML。在这段时间内,客户端等待(并且可能会因为他正在经历的缓慢连接而诅咒他的ISP :-))。因此,一旦HTML到达客户端浏览器,javascript变量将设置其值。所以不需要告诉javascript等待,在服务器完成其工作并将HTML推送到客户端之前,javascript甚至不存在。

You shouldn't worry about this because first the server script executes, takes its time to communicate with the database, assigns the msgLive variable a value, and finally it generates the HTML which is sent to the client. During all this time the client waits (and probably curses his ISP for the slow connection he is experiencing :-)). So once the HTML reaches the client browser the javascript variable will have its value set. So no need to tell javascript to wait, javascript doesn't even exist until the server finishes its job and pushes the HTML to client.

这篇关于如何问javascript等待mysql为php变量赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆