PHP include breaks Javascript

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
staypuftman
Forum Newbie
Posts: 13
Joined: Tue Jun 13, 2006 1:36 pm

PHP include breaks Javascript

Post by staypuftman »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello PHP forum-

I have a weird problem that I have uncovered.  It seems that my php include files render all my javascript references invalid.  I am redesigning a site and this is seriously screwing things up for me.

On one of my test sites, I have been debugging all my XHTML/CSS stuff in one HTML so I wouldnt have to worry about parsing the page apart during the debugging phase.  Now I have the skeleton of the page relatively bug free (I mean you can never account for IE completely), I want to apply my test site over to my template system in movable type.  The way I have done this in the past is by using little php files for each part of my site (header, footer, etc) and then in the main page I just php include them all together.  In my new design the structure is the same, but javascript is playing a much bigger role so this is a real problem now.

The links at the bottom of the header in all the following examples SHOULD produce a nifty little sliding javascript menu - which is how I know the code isnt working on the parsed version (tested on FF1.5, IE6 and Opera 9).  I have done a number of checks and there is no real difference in the code, except the fact that Im doing a php include for the header in the last rendition.

Non-parsed purely HTML version outside of the MT system (the base code for the other pages - check the view source to see code)
http://www.humanproductivitylab.com/tes ... _test.html

The non-parsed version run through the MT engine (shows MT is NOT the problem, like I thought it might be):
http://www.humanproductivitylab.com/tes ... nscopy.php

The php parsed version run through the MT framework:
http://www.humanproductivitylab.com/test/

Here is the code of the parsed version because maybe Im just missing something real simple:

Code: Select all

<?php
	//included code for page header
	include ('/home/hplmt/public_html/test/header.inc.php'); 
?> 

		<div id="container_content">
			<div id="content_primary">
                             [content ommitted to save space]
			</div>
			<?php
				//included code for rightnav column
				include ('/home/hplmt/public_html/test/rightnav.inc.php'); 
			?>
		</div>
<?php
	//included code for page footer
	include ('/home/hplmt/public_html/test/footer.inc.php'); 
?>
I know the remote JS reference to the transmenu script in the header is correct because the remote stylesheet reference is working (and the transmenu script folder is on the same level as the template folder holding the CSS file) - so we can also rule that out. Is there a history of php includes breaking javascript that Im not aware of? If anybody can lend a hand, I would be most appreciative. I think our server is running PHP5 but Im not exactly sure which release.

thanks
john serrao


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
arkady
Forum Newbie
Posts: 23
Joined: Sun Sep 17, 2006 9:34 pm

Post by arkady »

running the two output source codes with diff produces:

Code: Select all

<script type="text/javascript" src="/test/scripts/mt/mt-site.js"></script>
AND
<script type="text/javascript" src="/scripts/mt/mt-site.js"></script>
Noting the extra "/test" that could be the problem.
staypuftman
Forum Newbie
Posts: 13
Joined: Tue Jun 13, 2006 1:36 pm

Good thought but same problem persists

Post by staypuftman »

The difference you are pointing out pertains to when I was debugging to see if the problem was just in the path to the file. Turns out that file isnt even being referenced - its the 'transmenu_mod.js' file being referenced. Any other ideas?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

According to Firebug..
reference to undefined property childMenus transmenu_mod.js (line 483)
staypuftman
Forum Newbie
Posts: 13
Joined: Tue Jun 13, 2006 1:36 pm

Hmmmm

Post by staypuftman »

If that is the case, why would the script work on 'whole' pages but break on PHP parsed pages? Why would breaking the page up change the functionality of the script?
staypuftman
Forum Newbie
Posts: 13
Joined: Tue Jun 13, 2006 1:36 pm

Firebug

Post by staypuftman »

I downloaded Firebug to try and see the error you are seeing - I am not seeing it. In my Firebug on FF 1.5.0.7 I see a green check in the the status bar, which I would take to mean there are no errors.

The function in question is here:

Code: Select all

function toString() {
                var aHtml = [];
                var sClassName = "transMenu" + (oActuator.constructor != TransMenuItem ? " top" : "");

                for (var i = 0, item = null; item = this.items[i]; i++) {
                        aHtml[i] = item.toString(childMenus[i]);
                }

                return '<div id="' + id + '" class="' + sClassName + '">' + 
                        '<div class="content"><table class="items" cellpadding="0" cellspacing="0" border="0">' + 
                        '<tr><td colspan="2"><img src="' + TransMenu.spacerGif + '" width="1" height="' + TransMenu.menuPadding + '"></td></tr>' + 
                        aHtml.join('') + 
                        '<tr><td colspan="2"><img src="' + TransMenu.spacerGif + '" width="1" height="' + TransMenu.menuPadding + '"></td></tr></table>' + 
                        '<div class="shadowBottom"><img src="' + TransMenu.spacerGif + '" width="1" height="1"></div>' + 
                        '<div class="shadowRight"><img src="' + TransMenu.spacerGif + '" width="1" height="1"></div>' + 
		        '<div class="background"><img src="' + TransMenu.spacerGif + '" width="1" height="1"></div>' + 
	                '</div></div>';
        }
}
Further up in the code, childmenus gets defined:

Code: Select all

var childMenus = [];
This all seems copacetic to me, no?
staypuftman
Forum Newbie
Posts: 13
Joined: Tue Jun 13, 2006 1:36 pm

...

Post by staypuftman »

So I guess the final verdict is the php include is somehow unmasking a javascript error I cant see? Anybody have anything else?
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

I dont know why this hasn't been said before (unless i missed it, or I'm wrong...), but php cant really "cause" any problems with javascript... as PHP is server side code and JS is client side... however, if your php created javascript is not outputting the correct code, then that could be the cause of the problem.

Take the full source code of the page that works...
Take the full source code (use the 'View Source' in your browser) of the php generated page


Compare them both... There will probably be a difference...
staypuftman
Forum Newbie
Posts: 13
Joined: Tue Jun 13, 2006 1:36 pm

JS conflict

Post by staypuftman »

Well, after tons of debugging, it turns out that two of my javascrpts somehow disable each other. The ritecounter script bumps into the transmenu script...weird but true. Thanks for the help out there in forum land.
sujon
Forum Newbie
Posts: 2
Joined: Thu Oct 12, 2006 12:50 am

PHP include breaks Javascript

Post by sujon »

I'll help u privately. If u feel interest then inform me.
sujon :idea:
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: PHP include breaks Javascript

Post by jayshields »

sujon wrote:I'll help u privately. If u feel interest then inform me.
sujon :idea:
1) He's resolved the issue; 2) Forums are here for everybody, if you give him a solution via PM and someone else comes across this same problem they will have to ask again because your solution wouldn't be public.
Post Reply