Structure between PHP and HTML
Moderator: General Moderators
-
registereduser
- Forum Newbie
- Posts: 21
- Joined: Wed Nov 04, 2009 2:09 pm
Structure between PHP and HTML
I have a file called index.php as listed follows. I got error message that variable colorUser is not defined. Could anyone tell me what is wrong with my structure of this file? Additional information is everthing between <script type='text/javascript'> and </script> have been treated neither as javascript script nor as HTML code, but as text. This additional information comes from the font color in Firefox Error Console.
<?php
$u = [something, which comes from database]
?>
<html>
<head>
<script type='text/javascript'>
var colorUser = "<?php echo $u; ?>";
</script>
</head>
<body>
[Some HTML codes]
</body>
</html>
<?php
$u = [something, which comes from database]
?>
<html>
<head>
<script type='text/javascript'>
var colorUser = "<?php echo $u; ?>";
</script>
</head>
<body>
[Some HTML codes]
</body>
</html>
Re: Structure between PHP and HTML
Have you tried looking at your source code once the page loads, to check if colorUser has a value?
Should have something like this in your browsers source:
Also, please wrap your code using the code or php tags.
General Posting Guidelines - In a nutshell: #9
Code: Select all
var colorUser = "<?php echo $u; ?>";Code: Select all
var colorUser = "ImExampleText";General Posting Guidelines - In a nutshell: #9
-
registereduser
- Forum Newbie
- Posts: 21
- Joined: Wed Nov 04, 2009 2:09 pm
Re: Structure between PHP and HTML
Yes, Weiry. I have checked the browser source and find nothing wrong. What I do not understand is, the color of in FF Error Console is black, which indicate FF does not regarding this part as HTML.
Are you saying it looks no problem in structure? IE does not work with it either.
Are you saying it looks no problem in structure? IE does not work with it either.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Try putting double-quotes around "text/javascript".
Also, sometimes, I quickly re-type a small portion of my code to demonstrate my problem and obfuscate my ultimate goal (ruling the world, obviously), but that code works fine. If you did that, you might want to go through your HTML and check for syntax errors.

Also, sometimes, I quickly re-type a small portion of my code to demonstrate my problem and obfuscate my ultimate goal (ruling the world, obviously), but that code works fine. If you did that, you might want to go through your HTML and check for syntax errors.
-
registereduser
- Forum Newbie
- Posts: 21
- Joined: Wed Nov 04, 2009 2:09 pm
Re:
Hi Jonah
Thank you so much for help. After changed to double-quotes, it still same. What I found out now is anything between that javascript becomes black color, even comments. It is norm for FF. It indicates my structure might be ok. The problems still lie somewhere else.
Thank you so much for help. After changed to double-quotes, it still same. What I found out now is anything between that javascript becomes black color, even comments. It is norm for FF. It indicates my structure might be ok. The problems still lie somewhere else.
Jonah Bron wrote:Try putting double-quotes around "text/javascript".
Also, sometimes, I quickly re-type a small portion of my code to demonstrate my problem and obfuscate my ultimate goal (ruling the world, obviously), but that code works fine. If you did that, you might want to go through your HTML and check for syntax errors.
-
registereduser
- Forum Newbie
- Posts: 21
- Joined: Wed Nov 04, 2009 2:09 pm
Re: Structure between PHP and HTML
Sorry, my original submission does not make it clearer:
variable colorUser is used by global.js and the "undefined" error message was generated by global.js. If the main HTML index was static (not generated by PHP), this structure does work ok. However, if main HTML page was generated by PHP, it is not ok.
Code: Select all
<?php
$u = [something, which comes from database]
?>
<html>
<head>
<script language='Javascript' src='global.js' type='text/javascript'></script>
<script type='text/javascript'>
var colorUser = "<?php echo $u; ?>";
</script>
</head>
<body>
[Some HTML codes]
</body>
</html>
- iankent
- Forum Contributor
- Posts: 333
- Joined: Mon Nov 16, 2009 4:23 pm
- Location: Wales, United Kingdom
Re: Structure between PHP and HTML
Swap them around 
in the original you're opening global.js before declaring colorUser, so colorUser isn't accessible by any inline code in global.js (functions are probably ok unless they're called by functions inline in global.js)
Code: Select all
<script type='text/javascript'>
var colorUser = "<?php echo $u; ?>";
</script>
<script language='Javascript' src='global.js' type='text/javascript'></script>
-
registereduser
- Forum Newbie
- Posts: 21
- Joined: Wed Nov 04, 2009 2:09 pm
Re: Structure between PHP and HTML
I moved window.onload() from global.js to PHP, it still shows "undefined" error.
- iankent
- Forum Contributor
- Posts: 333
- Joined: Mon Nov 16, 2009 4:23 pm
- Location: Wales, United Kingdom
Re: Structure between PHP and HTML
we don't know what global.js is so we have no idea what that means, regardless, undefined means just that, the variable hasn't yet been defined.
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
-
registereduser
- Forum Newbie
- Posts: 21
- Joined: Wed Nov 04, 2009 2:09 pm
Re: Structure between PHP and HTML
Thank you guys. The problem was a bug in codes. This structure is sound. Working very well now.