php / javascript header include issue

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
User avatar
swiftouch
Forum Commoner
Posts: 80
Joined: Sun Dec 10, 2006 7:35 am
Location: Salt Lake City, Utah

php / javascript header include issue

Post by swiftouch »

This is for all users out there who wonder why your javascripts aren't available when including them in this manner:

<script src="scripts/formvalidator.js"></script>

When trying to include them this way it doesn't work.

Code: Select all

<?php
function print_header() {
?>

<html>
<head>
<script src="scripts/formvalidator.js"></script>
</head>
<body>

<?php }?>
but...

If i just include them using php include= "scripts/formvalidator.js";

it works.

Why?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The first way has always worked for me. Using include() to include something that contains no PHP code to execute is generally wasteful.
User avatar
swiftouch
Forum Commoner
Posts: 80
Joined: Sun Dec 10, 2006 7:35 am
Location: Salt Lake City, Utah

Post by swiftouch »

For some reason I couldn't get it to work. Dont ask me why. I "tailed" the error logs. I followed the links. I can only think that somehow this server is different from my others in some obscure way that caused me to waste 2 hours of my time.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

How was it not working for you?
User avatar
swiftouch
Forum Commoner
Posts: 80
Joined: Sun Dec 10, 2006 7:35 am
Location: Salt Lake City, Utah

Post by swiftouch »

Say i have a php header:

Code: Select all

<?php
function print_header() {
?>
<html>
<head>
<script src="scripts/formvalidator.js" language="javascript" type="text/javascript"></script>
<script src="scripts/images.js" language="javascript" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="my.css">
</head>
<body>
<?php }?>
When i utilize the function like this:

Code: Select all

print_header();
I'm unable to access the javascript. My forms don't validate and my image hovers don't work.
If I just paste the javascript, without including, it works. If in the header I use this, it works:

Code: Select all

include "scripts/javascript.js";
Pretty much I don't understand how including via php works instead of via HTML <script src="yada.js"> tag
I hope I better explained it this time.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Just for sanity checking, can you call the header function again, and this time view the source of the output page to make sure the header function is actually outputting.
User avatar
swiftouch
Forum Commoner
Posts: 80
Joined: Sun Dec 10, 2006 7:35 am
Location: Salt Lake City, Utah

Figured it out

Post by swiftouch »

I've had that header problem for some time and I finally had enough extra time today to really get it working the way it should.

As it turns out there were two problems. The first was the server permissions.

I had to "chown" the include path or nothing was going to work.

The second, when including a javascript like this:

<script src="scripts/yada.js"></script>

...within the yada.js file, you can't have <script></script> tags surrounding the javascript.


This is a direct difference from php where everything has to be surrounded in php tags when including files

I was convinced it was a php problem. I was wrong.

Thanks for all your help.
Post Reply