Results 1 to 7 of 7

Thread: How to put JS in PHP

  1. #1
    Join Date
    Jan 2009
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How to put JS in PHP

    I got a piece of JS code from Dynamic http://www.dynamicdrive.com/dynamici...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? 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>

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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 Code:
    <?php echo "<script type=\"text/javascript\" src=\"myjavascript.js\"></script>; ?>
    Hope that helps.

  3. #3
    Join Date
    Jan 2009
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Schmoopy View Post
    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 Code:
    <?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.

  4. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    I don't quite understand your second question, but you put the:

    PHP Code:
    <?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:

    PHP Code:
    <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>

  5. #5
    Join Date
    Jan 2009
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Schmoopy View Post
    I don't quite understand your second question, but you put the:

    PHP Code:
    <?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:

    PHP Code:
    <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/dynamici...iccalendar.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.

  6. #6
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Yes, it means put the code between the <head> tags, like so:

    Code:
    <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!

  7. The Following User Says Thank You to Schmoopy For This Useful Post:

    newest (01-27-2009)

  8. #7
    Join Date
    Jan 2009
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Schmoopy View Post
    Yes, it means put the code between the <head> tags, like so:

    Code:
    <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...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •