简体   繁体   中英

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. (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.

    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

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. The rest of it should be literal HTML or 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.

The above is for getting a PHP variable into a JS literal that contains HTML code. If just you want to get a PHP value into a Javascript variable, it's slightly different. Then you can use json_encode() to generate the JS representation:

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

UPDATE

this is a .js file, its taken from ajax chat

It won't work inside a .js file, because those files are not parsed by 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.


ORIGINAL ANSWER

This is not a proper PHP inside this line:

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

Or just to strip the PHP part:

<?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:

<?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.

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). You never answer about the filetype in the comments.

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