简体   繁体   English

我如何将php元素转换为JavaScript

[英]How do I get a php element into a javascript

I am scripting a chat for a forum, and it seems that it uses php to get the user's avatars. 我正在为论坛编写聊天脚本,似乎它使用php来获取用户的头像。 (PS idk anything about weather or not javascript can use sql databases or how to work with it so i would like to stick to php) But the problem is that the javascript isnt liking it if i put php variables into it. (PS idk关于天气的任何信息或非javascript都可以使用sql数据库或如何使用它,因此我想坚持使用php),但问题是,如果我将php变量放入其中,javascript就不喜欢它。

    getUserNodeString: function(userID, userName, userRole) {
    var encodedUserName, str;
    if(this.userNodeString && userID === this.userID) {
        return this.userNodeString;
    } else {
        encodedUserName = this.scriptLinkEncode(userName);
        str = '<div id="'
                + this.getUserDocumentID(userID)
                + '"><a href="javascript:ajaxChat.toggleUserMenu(\''
                + this.getUserMenuDocumentID(userID)
                + '\', \''
                + encodedUserName
                + '\', '
                + userID
                + ');" class="'
                + this.getRoleClass(userRole)
                + '" title="'
                + this.lang['toggleUserMenu'].replace(/%s/, userName)
                + '">'
                + userName
                + '</a><?php echo \'<img src="test.php" />\' ?>'
                + '<ul class="userMenu" id="'
                + this.getUserMenuDocumentID(userID)
                + '"'
                + ((userID === this.userID) ?
                    '>'+this.getUserNodeStringItems(encodedUserName, userID, false) :
                    ' style="display:none;">')
                + '</ul>'
                +'</div>';
        if(userID === this.userID) {
            this.userNodeString = str;
        }
        return str; 
    }
},
  • '</a><?php echo \\'<img src="test.png" />\\' ?>' is the line thet im trying to use, i havent put my variable yet, im trying it with a test immage first '</a><?php echo \\'<img src="test.png" />\\' ?>'是我试图使用的那行,我还没有放入变量,我用一个测试的immage对其进行了尝试第一

It should be like this: 应该是这样的:

+ '</a><img src="<?php echo htmlentities($src, ENT_DISALLOWED | ENT_QUOTES | ENT_HTML5) ?>" />'

You should only escape back into PHP for the specific part of the code that needs to contain the PHP variable. 对于只需要包含PHP变量的代码的特定部分,您应该只返回PHP。 The rest of it should be literal HTML or Javascript. 其余的应该是文字HTML或Javascript。

You should also use htmlspecialchars() to ensure that the variable content is encoded properly to be used in an HTML attribute, in case it contains special characters. 您还应该使用htmlspecialchars()来确保变量内容经过正确编码以在HTML属性中使用,以防它包含特殊字符。

The above is for getting a PHP variable into a JS literal that contains HTML code. 以上是将PHP变量转换为包含HTML代码的JS文字的方法。 If just you want to get a PHP value into a Javascript variable, it's slightly different. 如果只是想将PHP值放入Javascript变量中,则略有不同。 Then you can use json_encode() to generate the JS representation: 然后,您可以使用json_encode()生成JS表示形式:

var js_var = <?php echo json_encode($php_var) ?>;

UPDATE UPDATE

this is a .js file, its taken from ajax chat 这是一个.js文件,取自ajax聊天

It won't work inside a .js file, because those files are not parsed by PHP. 它无法在.js文件中运行,因为PHP无法解析这些文件。 It can be set up so that they get parsed, but I strongly suggest you don't do it , ever. 可以对其进行设置以便对其进行解析,但是我强烈建议您永远不要这样做

Besides, you don't need PHP for what you're doing. 此外,您不需要PHP即可完成工作。


ORIGINAL ANSWER 原始答案

This is not a proper PHP inside this line: 这行内的PHP不正确:

+ '</a><?php echo \'<img src="test.php" />\' ?>'

Or just to strip the PHP part: 或者只是剥离PHP部分:

<?php echo \'<img src="test.php" />\' ?>

To get it to work, you need to remove those escaped quotes (you don't need to worry about JS quotes, because PHP parser will hit the file first, leaving you with whatever you echoed), and add a semi-colon: 要使其正常工作,您需要删除那些转义的引号(您不必担心JS引号,因为PHP解析器将首先命中该文件,使您回显任何内容),并添加一个分号:

<?php echo '<img src="test.php" />'; ?>

and the full line should be: 整行应为:

    + '</a><?php echo '<img src="test.php" />'; ?>'

which will render as: 将呈现为:

    + '</a><img src="test.php" />'

It doesn't really make sense to use PHP for this, but okay. 为此,使用PHP确实没有意义,但是可以。

Of course, it has to be inside a file that the server will parse with PHP (.php files by default, strongly suggest you don't set up PHP parsing for JS files). 当然,它必须位于服务器将使用PHP解析的文件中(默认情况下为.php文件,强烈建议您不要为JS文件设置PHP解析)。 You never answer about the filetype in the comments. 您永远不会在注释中回答有关文件类型的问题。

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

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