Page 1 of 1

Structure between PHP and HTML

Posted: Wed Nov 18, 2009 3:09 pm
by registereduser
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>

Re: Structure between PHP and HTML

Posted: Wed Nov 18, 2009 3:37 pm
by Weiry
Have you tried looking at your source code once the page loads, to check if colorUser has a value?

Code: Select all

var colorUser = "<?php echo $u; ?>";
Should have something like this in your browsers source:

Code: Select all

var colorUser = "ImExampleText";
Also, please wrap your code using the code or php tags.
General Posting Guidelines - In a nutshell: #9

Re: Structure between PHP and HTML

Posted: Wed Nov 18, 2009 3:57 pm
by registereduser
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.

Posted: Wed Nov 18, 2009 4:29 pm
by Jonah Bron
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.

:)

Re:

Posted: Thu Nov 19, 2009 12:19 pm
by registereduser
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.
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.

:)

Re: Structure between PHP and HTML

Posted: Thu Nov 19, 2009 12:28 pm
by registereduser
Sorry, my original submission does not make it clearer:

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>
 
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.

Re: Structure between PHP and HTML

Posted: Thu Nov 19, 2009 12:31 pm
by iankent
Swap them around :)

Code: Select all

 
<script type='text/javascript'>
var colorUser = "<?php echo $u; ?>";
</script>
<script language='Javascript' src='global.js' type='text/javascript'></script>
 
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)

Re: Structure between PHP and HTML

Posted: Thu Nov 19, 2009 12:39 pm
by registereduser
I moved window.onload() from global.js to PHP, it still shows "undefined" error.

Re: Structure between PHP and HTML

Posted: Thu Nov 19, 2009 12:56 pm
by iankent
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.

Posted: Thu Nov 19, 2009 1:30 pm
by Jonah Bron
Do you mean you put a bit of javascript in a PHP file? You probably know this, but they're not interchangeable.

Re: Structure between PHP and HTML

Posted: Thu Nov 26, 2009 4:16 pm
by registereduser
Thank you guys. The problem was a bug in codes. This structure is sound. Working very well now.