简体   繁体   中英

php communicate with html and javascript

I heard people said php server site script cannot communicated with html and javascript client-side script. But when I test it, seem php can tell html and javascript what to do. 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. 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? 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. And PHP, which is on the server side.

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 .

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. But you can code like this and should not put all html to php function 'echo'. It's the same instead you don't need to trigger the php function. 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; 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.

It is however possible to communicate with PHP scripts through AJAX calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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