简体   繁体   English

php与html和javascript通信

[英]php communicate with html and javascript

I heard people said php server site script cannot communicated with html and javascript client-side script. 我听说人们说php服务器站点脚本无法与html和javascript客户端脚本进行通信。 But when I test it, seem php can tell html and javascript what to do. 但是当我测试它时,似乎php可以告诉html和javascript该怎么做。 Here is my codes : 这是我的代码:

<?php if ((isset($username1))&&($username1 == $username2)){ ?>
<div style="position:relative;">
    <img src="http://plekz.com/images/layouts/theme.png" onClick="showThemeDiv(); hideThemeTip();" style="margin-bottom:3px; margin-left:1px; position:relative;" onMouseOver="showThemeTip();" onMouseOut="hideThemeTip();" />
</div>
<?php }  ?>

I tested it on IE, Firefox and Chrome, all works perfectly. 我在IE,Firefox和Chrome上对其进行了测试,都可以完美运行。 But I still worry code this way will lead to problem when I move all the file to other online webhosting/server... Do I need to put all html and javascript codes into php echo? 但是我仍然担心,当我将所有文件移动到其他在线网站托管/服务器时,这种方式的代码会导致问题。是否需要将所有html和javascript代码放入php echo中? Or I can just code like this without having any problem in future? 还是我可以像这样编写代码,将来不会有任何问题? Is it standard way to code like this? 这样的标准编码方式吗?

What you are doing is just PHP, in your code there is no actual communication between "html and javascript", which is on the client's side. 您正在做的只是PHP,在您的代码中,“ html和javascript”之间没有实际的通信,这是在客户端方面。 And PHP, which is on the server side. 还有PHP,它在服务器端。

What people mean with communication is that the user can change something on the webpage dynamically without reloading the webpage. 人们交流的意思是,用户可以在不重新加载网页的情况下动态更改网页上的某些内容。 Such a thing can be done using AJAX . 可以使用AJAX完成这样的事情。

In short, the code you are using will work on any webserver and with any browser. 简而言之,您使用的代码可在任何网络服务器和任何浏览器上运行。

It's a bad way when html and php code are in one file and much better when separate. 当html和php代码位于一个文件中时,这是一种不好的方法,而在分开时则更好。 But you can code like this and should not put all html to php function 'echo'. 但是您可以像这样编写代码,不应将所有html都放入php函数'echo'中。 It's the same instead you don't need to trigger the php function. 是相同的,但是您不需要触发php函数。 And more much understandable to read a code, IMHO. 恕我直言,阅读代码更容易理解。

PHP is simply a scripting language which gets interpreted at runtime whenever a request is made; PHP只是一种脚本语言,在发出请求时就会在运行时进行解释。 people say it cannot communicate with HTML/Javascript because it is interpreted on the server side, it is not visible/accessible to the user agent once the page is rendered. 人们说它无法与HTML / Javascript进行通信,因为它是在服务器端进行解释的,一旦呈现页面,用户代理就无法看到/访问它。

It is however possible to communicate with PHP scripts through AJAX calls. 但是,可以通过AJAX调用与PHP脚本进行通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM