php已完成加载,然后仅开始加载javascript.是真的吗 [英] php is finish loaded then only start load javascript. Is it true?

查看:141
本文介绍了php已完成加载,然后仅开始加载javascript.是真的吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何让javascript等待mysql分配值给php变量?

Possible Duplicate:
How to ask javascript wait for mysql assign value to php variable?

我听说人们说php代码将完成加载,然后才开始加载javascript.信息位于如何询问javascript等待mysql将值分配给php变量?

I heard people said php codes will be finished loaded then only will start loading javascript. Info at How to ask javascript wait for mysql assign value to php variable?

但是当我写信时:

<script type="text/javascript">
alert("<?php echo $username; ?>");
</script>
<?php $username = "abc"; ?>

它无法警告php变量的值.这是因为首先加载javascript,然后才开始加载php.那么服务器站点脚本的事实已经完成加载,那么只有启动加载客户端站点脚本是错误的吗?

It fail to alert the value of php variable. It is because javascript is loaded at first, then only start loading php. So the fact of server-site script is finish loaded then only start load client-site script is wrong?

如果可以在php之前执行javascript,那么我有问题要让javascript等待mysql将值分配给php变量.请在上帮助我解决此问题.如何要求javascript等待mysql将值分配给php变量?

If javascript can be executed before php, then I have problem to ask javascript wait for mysql assign value to php variable. Please help me to solve this problem at How to ask javascript wait for mysql assign value to php variable?

推荐答案

PHP在Web服务器上执行,从而生成一个html(或其他内容)页面供浏览器显示. JavaScript是在客户端的用户浏览器中执行的,因此,按照定义,php已在JavaScript执行之前完成了执行.

PHP is executed on the web-server, generating an html (or whatever) page to be displayed by the browser. JavaScript is executed on the client-side, in the user's browser, therefore php has, by definition, finished executing before JavaScript executes.

您似乎假设在同一位置中运行了php,然后运行了JavaScript.这是一个谬论;您遇到的问题是您将JavaScript变量分配给了$username变量 之前的值,而php脚本已对该变量进行了赋值/求值;应该在服务器上从以下哪个进行转换:

You seem to be assuming that php runs, then JavaScript is run, in the same place. Which is a fallacy; the problem you're having is that you assign the JavaScript variable to the value of the $username variable before the php script has assigned/evaluated the variable; which should transform, on the server, from:

<script type="text/javascript">
alert("<?php echo $username; ?>");
</script>
<?php $username = "abc"; ?>

收件人,在客户端上:

<script type="text/javascript">
alert("");
</script>

因此,尽管您的脚本似乎什么也不做,或者做错了事,但它确实在按照您的要求执行.您应该尝试将代码重新排列一下以达到以下目的:

Therefore, while your script appears to do nothing, or do the wrong thing, it's doing exactly what you told it to do. You should try and rearrange your code a little to:

<?php $username = "abc"; ?>
<script type="text/javascript">
alert("<?php echo $username; ?>");
</script>

哪个允许php评估/分配变量,然后在alert()语句中设置/可用该变量.

Which allows php to evaluate/assign the variable, and then have the variable set/available in the alert() statement.

这篇关于php已完成加载,然后仅开始加载javascript.是真的吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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