Page 1 of 1

php / javascript header include issue

Posted: Thu Jul 12, 2007 5:04 pm
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?

Posted: Thu Jul 12, 2007 5:58 pm
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.

Posted: Thu Jul 12, 2007 6:19 pm
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.

Posted: Thu Jul 12, 2007 6:25 pm
by RobertGonzalez
How was it not working for you?

Posted: Fri Jul 13, 2007 10:58 am
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.

Posted: Fri Jul 13, 2007 11:11 am
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.

Figured it out

Posted: Tue Jul 24, 2007 3:15 pm
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.