Log in

View Full Version : How to put JS in PHP



newest
01-27-2009, 05:42 PM
I got a piece of JS code from Dynamic http://www.dynamicdrive.com/dynamicindex14/flexislide.htm. (I attached the code in the bottom of my message)


What it does is put the code anywhere in the body that I want the silde to show. I have a something.php and a something.tpl. Where do I put this piece of code? In php or tpl? I have tried to run the codes in the html, it works perfectly. But when I put it in the tpl file, I got a blank screen. What have I done wrong? :confused: Could anyone please help? Thanks. Really appreciated.

<script language="JavaScript1.2">

/***********************************************
* Flexi Slideshow- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var variableslide=new Array()

//variableslide[x]=["path to image", "OPTIONAL link for image", "OPTIONAL text description (supports HTML tags)"]

variableslide[0]=['ball.gif', '', '']
variableslide[1]=['spaceship.gif', 'http://www.space.com', 'Has aliens landed on earth? You decide.']
variableslide[2]=['cake.gif', '', '']

//configure the below 3 variables to set the dimension/background color of the slideshow

var slidewidth='130px' //set to width of LARGEST image in your slideshow
var slideheight='120px' //set to height of LARGEST iamge in your slideshow, plus any text description
var slidebgcolor='#F3F3F3'

//configure the below variable to determine the delay between image rotations (in miliseconds)
var slidedelay=3000

////Do not edit pass this line////////////////

var ie=document.all
var dom=document.getElementById

for (i=0;i<variableslide.length;i++){
var cacheimage=new Image()
cacheimage.src=variableslide[i][0]
}

var currentslide=0

function rotateimages(){
contentcontainer='<center>'
if (variableslide[currentslide][1]!="")
contentcontainer+='<a href="'+variableslide[currentslide][1]+'">'
contentcontainer+='<img src="'+variableslide[currentslide][0]+'" border="0" vspace="3">'
if (variableslide[currentslide][1]!="")
contentcontainer+='</a>'
contentcontainer+='</center>'
if (variableslide[currentslide][2]!="")
contentcontainer+=variableslide[currentslide][2]

if (document.layers){
crossrotateobj.document.write(contentcontainer)
crossrotateobj.document.close()
}
else if (ie||dom)
crossrotateobj.innerHTML=contentcontainer
if (currentslide==variableslide.length-1) currentslide=0
else currentslide++
setTimeout("rotateimages()",slidedelay)
}

if (ie||dom)
document.write('<div id="slidedom" style="width:'+slidewidth+';height:'+slideheight+'; background-color:'+slidebgcolor+'"></div>')

function start_slider(){
crossrotateobj=dom? document.getElementById("slidedom") : ie? document.all.slidedom : document.slidensmain.document.slidenssub
if (document.layers)
document.slidensmain.visibility="show"
rotateimages()
}

if (ie||dom)
start_slider()
else if (document.layers)
window.onload=start_slider

</script>

<ilayer id="slidensmain" width=&{slidewidth}; height=&{slideheight}; bgColor=&{slidebgcolor}; visibility=hide><layer id="slidenssub" width=&{slidewidth}; left=0 top=0></layer></ilayer>

<p align="center"><font face="Arial" size="-2">Free DHTML scripts provided by<br>
<a href="http://www.dynamicdrive.com">Dynamic Drive</a></font></p>

Schmoopy
01-27-2009, 07:26 PM
The page is expecting PHP code, and not javascript, if you wanted to execute javascript on the PHP page you'll need to echo it - something like this:

<?php echo "<script type=\"text/javascript\">
function javascript{
bla bla put all your javascript here .com
}
making sure to precede all " with \ to not end the echo too early, and then when you're at the end of the script put a " to finish
?>

You'll want to put the javascript code in an external file to make it easier for you, then you all need to do is:



<?php echo "<script type=\"text/javascript\" src=\"myjavascript.js\"></script>; ?>


Hope that helps.

newest
01-27-2009, 08:08 PM
The page is expecting PHP code, and not javascript, if you wanted to execute javascript on the PHP page you'll need to echo it - something like this:

<?php echo "<script type=\"text/javascript\">
function javascript{
bla bla put all your javascript here .com
}
making sure to precede all " with \ to not end the echo too early, and then when you're at the end of the script put a " to finish
?>

You'll want to put the javascript code in an external file to make it easier for you, then you all need to do is:



<?php echo "<script type=\"text/javascript\" src=\"myjavascript.js\"></script>; ?>


Hope that helps.

Okay, I put all of the JS codes without the <script type=\"text/javascript\"></script>; ?> in to a file called myjavascript.js Then where do I put <?php echo "<script type=\"text/javascript\" src=\"myjavascript.js\"></script>; ?>? Into .php or .tpl?

One more question. Sometimes some JS will say save this external JS, and then put x piece of code into the head of the page. and Y piece of codes into the body of the page. Since I am useing PHP, where in the head do I put the X piece of code to? .PHP or .TPL? Or put it in the header of the .tpl?

Thank you for your help.

Schmoopy
01-27-2009, 08:40 PM
I don't quite understand your second question, but you put the:



<?php echo "<script type=\"text/javascript\" src=\"myjavascript.js\"></script>; ?>

into a .php file.

So it would be like this (I know this is not W3 valid but I really can't be arsed to put in the doctype etc:



<html>
<head>

<?php echo "<script type=\"text/javascript\" src=\"myjavascript.js\"></script>; ?>
<title>My Page</title>
</head>
<body>

Hi this is my page

</body>

</html>

newest
01-27-2009, 09:29 PM
I don't quite understand your second question, but you put the:



<?php echo "<script type=\"text/javascript\" src=\"myjavascript.js\"></script>; ?>

into a .php file.

So it would be like this (I know this is not W3 valid but I really can't be arsed to put in the doctype etc:



<html>
<head>

<?php echo "<script type=\"text/javascript\" src=\"myjavascript.js\"></script>; ?>
<title>My Page</title>
</head>
<body>

Hi this is my page

</body>

</html>



Thanks for the reply.

What I mean for the 2nd question is in the example of http://www.dynamicdrive.com/dynamicindex7/basiccalendar.htm

It said put it in the head of the page. Where is the head? Head.php? or head section of any .php page?

Thanks.

Schmoopy
01-27-2009, 09:44 PM
Yes, it means put the code between the <head> tags, like so:



<head>
<style type="text/css">

.main {
width:200px;
border:1px solid black;
}

.month {
background-color:black;
font:bold 12px verdana;
color:white;
}

.daysofweek {
background-color:gray;
font:bold 12px verdana;
color:white;
}

.days {
font-size: 12px;
font-family:verdana;
color:black;
background-color: lightyellow;
padding: 2px;
}

.days #today{
font-weight: bold;
color: red;
}

</style>


<script type="text/javascript" src="basiccalendar.js">

/***********************************************
* Basic Calendar-By Brian Gosselin at http://scriptasylum.com/bgaudiodr/
* Script featured on Dynamic Drive (http://www.dynamicdrive.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

</script>
</head>


Hope that clears up where you have to put it. You don't need a file called head.php, just make sure the code is between the <head> tags. Good luck!

newest
01-27-2009, 10:49 PM
Yes, it means put the code between the <head> tags, like so:



<head>
<style type="text/css">

.main {
width:200px;
border:1px solid black;
}

.month {
background-color:black;
font:bold 12px verdana;
color:white;
}

.daysofweek {
background-color:gray;
font:bold 12px verdana;
color:white;
}

.days {
font-size: 12px;
font-family:verdana;
color:black;
background-color: lightyellow;
padding: 2px;
}

.days #today{
font-weight: bold;
color: red;
}

</style>


<script type="text/javascript" src="basiccalendar.js">

/***********************************************
* Basic Calendar-By Brian Gosselin at http://scriptasylum.com/bgaudiodr/
* Script featured on Dynamic Drive (http://www.dynamicdrive.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

</script>
</head>


Hope that clears up where you have to put it. You don't need a file called head.php, just make sure the code is between the <head> tags. Good luck!

Thank you , Thank you, and Thank you...:p