1

好的,你好。

所以在我下面的代码中,我使用 if 语句来检查用户是否是管理员,但问题是它没有检查它,它甚至没有调用它,并且在控制台中它说:“未捕获的语法错误:意外的标记 <"。我猜它会立即执行 PHP 代码,但这不是我想要的,我只是希望它把它放到 div 中。(并且由于它检查会话,它应该可以工作,因为我已经检查了会话是否包含有关 div 的内容。)

代码:

for (var x = 0; x < commentData.length; x++) {
    if(x!=(commentData.length-1)) {
        var commentSubData = commentData[x].split("^");
        commentDIV = commentDiv+"<p style='display:block'><font size='4' color='white'>"+commentSubData[(x+3)]+"</font><br><br><font size='2' color='lightgray'>Posted on <font color='lightblue'>"+commentSubData[(x+2)]+"</font> by <font color='lightblue'>"+commentSubData[(x+1)]+"</font></font>
<?
    if($poster == $_SESSION['user_forumname'] || $_SESSION['perm_removecomments'] == 1) {
?>
        &nbsp;<font color='red'><a onclick='dialog(<?=$post_id;?>, "+commentSubData[(x+0)]+")' title='Remove'><span class='glyphicon glyphicon-remove'></span></a></font>
<?
    }
?>
     </p>
     <hr>";
    }
}
4

2 回答 2

2

您不能在 JS 文件中使用 PHP 代码。如果您使用的是内联且不位于外部 JS 文件中的 JS 代码,则可以执行

<script>
 var forumname = "<?php echo $_SESSION['user_forumname']; ?>";
 for (var x = 0; x < commentData.length; x++) {
        if(x!=(commentData.length-1)) {
            var commentSubData = commentData[x].split("^");

            var commentDIV = commentDiv+"<p style='display:block'><font size='4' color='white'>"+commentSubData[(x+3)]+"</font><br><br><font size='2' color='lightgray'>Posted on <font color='lightblue'>"+commentSubData[(x+2)]+"</font> by <font color='lightblue'>"+commentSubData[(x+1)]+"</font></font>";
<?php
     if($poster == $_SESSION['user_forumname'] 
     || $_SESSION['perm_removecomments'] == 1) {
?>
            if(commentSubData[x+1] == forumname)
            {
                 var string = "&nbsp;<font color='red'><a onclick='dialog(<?php echo $post_id; ?>, "+commentSubData[(x+0)]+")' title='Remove'><span class='glyphicon glyphicon-remove'></span></a></font>";
<?php
     }
?>
                 var string2 = "</p>
                               <hr>";
                 //This is the final string
                 var final = commentDIV.concat(string).concat(string2);
            }
        }
    }
</script>
于 2013-09-22T06:39:19.863 回答
0

你能试试这个

for (var x = 0; x < commentData.length; x++) {
        if(x!=(commentData.length-1)) {
            var commentSubData = commentData[x].split("^");
            commentDIV = commentDiv+"
                <p style='display:block'>
                <font size='4' color='white'>"+commentSubData[(x+3)]+"</font>
                <br><br>
                <font size='2' color='lightgray'>Posted on <font color='lightblue'>"+commentSubData[(x+2)]+"</font> by <font color='lightblue'>"+commentSubData[(x+1)]+"</font></font>
                <?php
                if($poster == $_SESSION['user_forumname'] || $_SESSION['perm_removecomments'] == 1) {
                ?>
                &nbsp;<font color='red'><a onclick='dialog(<?=$post_id;?>, "+commentSubData[(x+0)]+")' title='Remove'><span class='glyphicon glyphicon-remove'></span></a></font>
                <?php
                }
                ?>
                </p>
                <hr>";
        }
    }

因为这可能是因为起始 PHP 标记可以写为<?php

于 2013-09-22T06:36:24.443 回答