Structure between PHP and HTML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
registereduser
Forum Newbie
Posts: 21
Joined: Wed Nov 04, 2009 2:09 pm

Structure between PHP and HTML

Post 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>
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Structure between PHP and HTML

Post 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
registereduser
Forum Newbie
Posts: 21
Joined: Wed Nov 04, 2009 2:09 pm

Re: Structure between PHP and HTML

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

:)
registereduser
Forum Newbie
Posts: 21
Joined: Wed Nov 04, 2009 2:09 pm

Re:

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

:)
registereduser
Forum Newbie
Posts: 21
Joined: Wed Nov 04, 2009 2:09 pm

Re: Structure between PHP and HTML

Post 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.
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Structure between PHP and HTML

Post 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)
registereduser
Forum Newbie
Posts: 21
Joined: Wed Nov 04, 2009 2:09 pm

Re: Structure between PHP and HTML

Post by registereduser »

I moved window.onload() from global.js to PHP, it still shows "undefined" error.
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Structure between PHP and HTML

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post 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.
registereduser
Forum Newbie
Posts: 21
Joined: Wed Nov 04, 2009 2:09 pm

Re: Structure between PHP and HTML

Post by registereduser »

Thank you guys. The problem was a bug in codes. This structure is sound. Working very well now.
Post Reply