Forum Moderators: coopster

Message Too Old, No Replies

How do I include javascript with php file?

         

rdruser

12:03 am on Apr 6, 2005 (gmt 0)

10+ Year Member



I need to get this show/hide layers [webmasterworld.com] function to work with php, but am not sure how I include it? Do I echo and print it like normal html or include it outside of the php code? Or is it best to include it in a seperate file?

ironik

12:22 am on Apr 6, 2005 (gmt 0)

10+ Year Member



Javascript is usually better kept in seperate files as it saves download times (browser caches the file).

You could use PHP to generate the javascript either in a seperate file (using fopen() fwrite() and fclose()), or in the current page exactly the same as you do with html, PHP doesn't care much what type of information you are outputting to the page.

rdruser

12:38 am on Apr 6, 2005 (gmt 0)

10+ Year Member



Javascript is usually better kept in seperate files as it saves download times (browser caches the file).

You could use PHP to generate the javascript either in a seperate file (using fopen() fwrite() and fclose()), or in the current page exactly the same as you do with html, PHP doesn't care much what type of information you are outputting to the page.

If it is in a seperate file does it matter where it goes in the php file?

Also, I tried adding javascript function right under the db connection and it didn't work, should it be at the top if included in the same file?

jatar_k

1:15 am on Apr 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the js function needs to be right where it should be if it was html only, the constructed html document that is served to the user needs to have the <script> tag in the head that pulls the js file.

You probably just need to break out of php mode?>, and then put it right there, either the script tag including the js file or the function itself.

php really has nothing to do with it.

IamStang

1:39 am on Apr 6, 2005 (gmt 0)

10+ Year Member



This is what I do (which is pretty much, the same as others above are telling you). Maybe it would work for you as well.

Open a text editor and insert:

<html>
<head>
<title>page title</title>
<script language="javascript" src="scriptname.js">
</script>
</script>
</head>
<body>

scriptname.js is a txt file containing your jscript code.

Then just do an include "whatever_you_named_the_above_html_file.html";

If you need different page titles, omit the <html><head><title>name</title> part and do the include right after declaring your title in your script.

Like I said, works for me.

Maybe I gave too much detail here. I know I learn best by getting basic instructions that I investigate myself. If I did, my appologies. (mods, just tell me to shut up. LOL).

Later!
IamStang

rdruser

3:46 am on Apr 6, 2005 (gmt 0)

10+ Year Member



I posted it in the header and when viewed online it appears as follows

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title> - </title>
<link href="style.css" rel="stylesheet" type="text/css">
<script>
function displaylyr1(){
nolyr2();
nolyr3();
lyr1.style.display='';
}
function displaylyr2(){
nolyr1();
nolyr3();
lyr2.style.display='';
}
function displaylyr3(){
nolyr1();
nolyr2();
lyr3.style.display='';
}
function nolyr1(){
lyr1.style.display='none';
}
function nolyr2(){
lyr2.style.display='none';
}
function nolyr3(){
lyr3.style.display='none';
}
</script>
</head>
<body>
...

and used the following code in the spot

<div style="display;" id="lyr1">
<p align="center">
<img border="0" src="" width="120" height="60"></p>
</div>

<div style="display: none;" id="lyr2">
<p align="center">
Hello</p>
</div>

<div style="display: none;" id="lyr3">
<p align="center">
Goodbye</p>
</div>

</TD>
</TR>
<TR>
<TD width="98" height="14" bgcolor="#EAEAEA">
<p align="center"><font size="1" face="Verdana">
<a style="text-decoration: none; color: #000000" href="javascript:displaylyr2();void(0);">Link</a></font></TD>
<TD>
<IMG SRC="images/progress-more_10.gif" WIDTH=1 HEIGHT=14 ALT=""></TD>
<TD COLSPAN=3 bgcolor="#797979">
<p align="center"><font size="1" color="#FFFFFF" face="Verdana">
<a href="javascript:displaylyr3()">Click</a></font></TD>

but it still doesn't work?

jatar_k

3:58 am on Apr 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try a proper js script tag

<SCRIPT type="text/javascript">

rdruser

4:04 am on Apr 6, 2005 (gmt 0)

10+ Year Member



I did before (and even copy and pasted your's to make sure I had no typos or errors) and it still didnt work.

ironik

5:12 am on Apr 6, 2005 (gmt 0)

10+ Year Member



Try changing the code you use to make it visible to 'block' instead of empty:

function displaylyr1(){
nolyr2();
nolyr3();
lyr1.style.display='block';
}

(I tested this with your code in IE 5.5 and it works ok)

You could also probably change your code so you can read it easier:

function display(obj){
obj.style.display='block';
}
function hide(obj)
{
obj.style.display='none';
}

then just call something like display('lyr1') to show it and hide('lyr1') to hide it. It also makes it easier for you to go ahead and add more of them without having to add more functions.

dmmh

8:00 pm on Apr 6, 2005 (gmt 0)

10+ Year Member



on a side not, it is totally possible to have PHP generate javascript, dont think you need it in this case, but you might need it some day, so you know ;)

an example:

$movie_genre_java.= "<script language=\"JavaScript\" type=\"text/javascript\">\n";
$movie_genre_java.= "<!--\n";
$movie_genre_java.= "var newGenre = \"\";\n";
$movie_genre_java.= "function genreAdd()\n";
$movie_genre_java.= "{\n";
$movie_genre_java.= "newGenre+='<select name=\"genre[]\" size=\"1\" class=\"menu\">';\n";
$movie_genre_java.= "newGenre+='<option value=\"\" selected>select</option>';\n";
{
//a query to see what genres the user is allowed to list movies on
@mysql_select_db('db');
$query = 'SELECT genre FROM table WHERE genre IS NOT NULL ORDER BY genre ASC';
$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
$genres = array(); // initialize the genres array
while ($myresult = mysql_fetch_array($result)) // loop through and add each genre to genres array
{
$genres[]= $myresult;
}
foreach ($genres as $genre)
{
// build the options...
$movie_genre_java.= "newGenre+='<option value=\"$genre[0]\"";
$movie_genre_java.= ">$genre[0]</option>';\n";
}
// close the select tags
$movie_genre_java.= "newGenre+='</select><br/>';\n";
$movie_genre_java.= "document.getElementById('genre_container').innerHTML = newGenre;\n";
$movie_genre_java.= "}\n";
$movie_genre_java.= "-->\n";
$movie_genre_java.= "</script>\n"; }

use it as 'echo $movie_genre_java;' where you would normally put the javascript in a HTNL document ;)

rdruser

5:36 am on Apr 8, 2005 (gmt 0)

10+ Year Member



If i do an include to a seperate php file, should the javascript go in the header file or the individual php file being included?

rdruser

1:21 am on Apr 9, 2005 (gmt 0)

10+ Year Member



?