Computer Hope

Internet & Networking => Web design => Topic started by: newfunland on November 07, 2017, 12:02:52 PM

Title: settting php var to html var, Uncaught SyntaxError: Unexpected token var
Post by: newfunland on November 07, 2017, 12:02:52 PM
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
Title: Re: settting php var to html var, Uncaught SyntaxError: Unexpected token var
Post by: BC_Programmer 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']); ?>";
Title: Re: settting php var to html var, Uncaught SyntaxError: Unexpected token var
Post by: newfunland 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.