混合php和javascript - 在什么条件下? [英] Mixing php and javascript -- under what conditions?

查看:82
本文介绍了混合php和javascript - 在什么条件下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么条件下我可以将PHP放入Javascript代码中?总是好的吗?这只是一个坏主意吗?我很惊讶我还没有看到它。

Under what conditions can I put PHP into Javascript code? Is it always ok? Is it just a bad idea? I am surprised I have not seen it more.

例如,我遇到过这个..

For example, I came across this..

$('#thumbnail').imgAreaSelect({aspectRatio: '<?php echo $thumb_height/$thumb_width;?>', onSelectChange:preview}); 

..这是一个onpage脚本元素。我不是那么有经验,但我很惊讶我之前从未见过混合js / php。但是你也可以把PHP放在.js文件中吗?如果我将文件扩展名设置为.php,是否会解析文件?

..which was in an onpage script element. I'm not so experienced, but I was just surprised that I had never seen mixed js/php before. But can you put PHP in .js files as well? If I set the file extension to .php, will the file be parsed?

推荐答案

您可以将PHP代码放入 * .js 文件但你需要告诉网络服务器也在 *。js 文件上执行PHP解释器。这样的东西应该在你的 httpd.conf

You can put PHP code into *.js files but you need to tell the webserver to also execute the PHP interpreter on *.js files. Something like this should be in your httpd.conf

<IfModule php5_module>
    AddType application/x-httpd-php .php .php5 .js

    ...
</IfModule>

更好的解决方案是重命名 *。js 文件到 * .js.php 或只是 * .php 。浏览器不介意,PHP解释器也将在那些上执行。

A better solution is to just rename your *.js files to *.js.php or just *.php. The browsers won't mind and the PHP interpreter will also get executed on those.

这篇关于混合php和javascript - 在什么条件下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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