Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: settting php var to html var, Uncaught SyntaxError: Unexpected token var  (Read 1257 times)

0 Members and 1 Guest are viewing this topic.

newfunland

    Topic Starter


    Greenhorn

    • Experience: Experienced
    • OS: Other
    hi guys been searching for the past couple day trying to figure out whats wrong, if i stick this line of code anywhere outside of the html im running it works fine the variable is set to what it was expected to

    var userID = <?php echo ($stats['displayname']); ?>

    but if i stick it inside my code like below i get an error of Uncaught SyntaxError: Unexpected token var

    Code: [Select]
    script>
     var userID = <?php echo ($stats['displayname']); ?>
        var gamescore = new score.Submit(publicsitekey, teamName , userID{
        })
    </script>


    when viewing the page pages html after running it looks like this


    Code: [Select]
    var userID = Marmalade var gamescore = new Score.Submit('8VDCBKu05bEAHebtB0pgqWdcEB60B5kA', 'RedTeam', userID{

    })

    i sure it a syntax issue  where i forgot something in the var, or its not breaking and not reading a new line. but i cant figure it out for the life of me, any help is appreciated,  or just the most simple easiest way to set an html var from a php var, without the user having to do anything, i have also tried adding a ; after ?> but it just add ; to the user display name

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1074
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 8
    Re: settting php var to html var, Uncaught SyntaxError: Unexpected token var
    « Reply #1 on: November 07, 2017, 12:41:35 PM »
    Code: [Select]
    var userID = Marmalade

    First problem is the lack of a semicolon to terminate the statement. The var afterwards makes no sense to the interpreter because of this, particularly as there is no newline.

    But that isn't the only issue. The statement would assign the variable userID to the value of the *variable* Marmalade. I'm going to presume that you want to set the value of userID to the String "Marmalade" here? In that case you need quotes:
    Code: [Select]
    var userID = "<?php echo ($stats['displayname']); ?>";
    I was trying to dereference Null Pointers before it was cool.

    newfunland

      Topic Starter


      Greenhorn

      • Experience: Experienced
      • OS: Other
      Re: settting php var to html var, Uncaught SyntaxError: Unexpected token var
      « Reply #2 on: November 07, 2017, 11:39:01 PM »
      thanks for they reply, and info.  yes that is what i was trying to do, and it is working, that seems to be my biggest problem is when and where to use certain things.