简体   繁体   English

PHP中的外部.js文件语法问题

[英]PHP in external .js file syntax issue

I looked around but couldn't find an answer to my current issue. 我环顾四周,但找不到当前问题的答案。

I want to echo a PHP variable into my external .js file. 我想将PHP变量回显到我的外部.js文件中。 I then use the PHP variable in a javascript variable and from here use ajax to send the variable/s to my database. 然后,我在JavaScript变量中使用PHP变量,然后从此处使用ajax将变量发送到我的数据库。 I believe I am using incorrect syntax for the php echo. 我相信我对php echo使用了不正确的语法。 My javascript 我的JavaScript

content = '<div class="post">' + '<img src="<? echo $pic ?>" />' + '</div>';

Is this possible to echo the PHP variable to the external .js file? 是否可以将PHP变量回显到外部.js文件? Am I using the right syntax in the javascript variable? 我在javascript变量中使用了正确的语法吗?

Your server will not parse the .js file as PHP. 您的服务器不会将.js文件解析为PHP。 You could name your js file as a .php file, and then use header("Content-type: application/javascript") . 您可以将js文件命名为.php文件,然后使用header("Content-type: application/javascript")

Normally, your httpd is only set to parse files named 'foo.php' for PHP code; 通常,您的httpd仅设置为解析PHP代码名为“ foo.php”的文件; name the file foo.php instead of foo.js and manually set the content type: 将文件命名为foo.php而不是foo.js并手动设置内容类型:

<?php header('Content-type: application/javascript');?>
content = '<div class="post">' + '<img src="<? echo $pic ?>" />' + '</div>';

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

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