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
swiftouch
Forum Commoner
Posts: 80 Joined: Sun Dec 10, 2006 7:35 am
Location: Salt Lake City, Utah
Post
by swiftouch » Thu Jul 12, 2007 5:04 pm
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?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Jul 12, 2007 5:58 pm
The first way has always worked for me. Using include() to include something that contains no PHP code to execute is generally wasteful.
swiftouch
Forum Commoner
Posts: 80 Joined: Sun Dec 10, 2006 7:35 am
Location: Salt Lake City, Utah
Post
by swiftouch » Thu Jul 12, 2007 6:19 pm
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.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Thu Jul 12, 2007 6:25 pm
How was it not working for you?
swiftouch
Forum Commoner
Posts: 80 Joined: Sun Dec 10, 2006 7:35 am
Location: Salt Lake City, Utah
Post
by swiftouch » Fri Jul 13, 2007 10:58 am
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:
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:
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.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Fri Jul 13, 2007 11:11 am
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.
swiftouch
Forum Commoner
Posts: 80 Joined: Sun Dec 10, 2006 7:35 am
Location: Salt Lake City, Utah
Post
by swiftouch » Tue Jul 24, 2007 3:15 pm
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.