1

Okay, hi.

So in the code i have below i'm using an if statement to check if the user is an administrator, but the problem is that it doesn't check it, it doesn't even call it and in the console it says: "Uncaught SyntaxError: Unexpected token <". I'm guessing that it executes the PHP code instantly, but that's not what i want, i just want it to put it into the div. (And since it checks the session, it should work, since i've already checked if session contains those things about the div.)

Code:

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>";
    }
}

2 Answers 2

2

You cannot use PHP code inside of a JS file. If you're using JS code that's inline and isn't located inside of an external JS file, you can do

<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>
Sign up to request clarification or add additional context in comments.

7 Comments

One thing, i just realized that "$poster" in the if statement is supposed to be "commentSubData[(x+1)]", how would i add that?
You can't mix identify a JS variable with PHP. I'll update my answer to show you
I get the same error as before: "Uncaught SyntaxError: Unexpected token <".
EDIT: It was appearently the <hr> on the end string. When fixed it said: "Uncaught SyntaxError: Unexpected token else" on the } else {.
What does the error log say? If you're requiring this file from another one, then it might be the other file that's giving you the error. Check this JS console in chrome too!
|
0

Can you try this

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>";
        }
    }

As this may be because the starting PHP tag can be written as <?php

1 Comment

As said by @Lance you need to put your javascript code inside a .php file inside <script></script> tag!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.