+ Reply to Thread
Results 1 to 6 of 6

Thread: include php within javascript

  1. #1
    thezone1 is offline x10 Sophmore thezone1 is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    192

    include php within javascript

    Hi i have just setup prototype window class,
    but no i need to display php output in the windows,
    ( i want to include php in javascript.js )

    so does anyone know how i can do this..

    thanks

  2. #2
    supajason's Avatar
    supajason is offline x10 Lieutenant supajason is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    England........:-)
    Posts
    288

    Re: include php within javascript

    this is an example:

    main page:
    PHP Code:
    <style type="text/css" media="screen">
      <?php include('./includes/layout.php'); ?></style>
    layout.php:
    PHP Code:
    body,td,th {
        font-family:Verdana, Arial, Helvetica, sans-serif;
        font-size:12px;
    }
    <?PHP echo "hello"?>
    off the top of my head anyway?
    Last edited by supajason; 04-24-2008 at 09:46 AM.
    supajason was here

  3. #3
    thezone1 is offline x10 Sophmore thezone1 is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    192

    Re: include php within javascript

    sorry i did try it, but nothing i think its because im running it in a .js file and not .php so anymore ideas guys ??

  4. #4
    FireLancer is offline x10Hosting Member FireLancer is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    15

    Re: include php within javascript

    Php is run ont he server, all php code is then removed and the reamining page sent to the client
    Java Script is not ran on the server but just sent to the client. This means that:

    php can generate java script (because php is run first)
    javascript cannot generate php as it has already left the server.
    eg this works:
    Code:
    </php
     echo
       "<script type=\"text/javascript\">\n".
       " document.write(\"Hello World!\");\n".
       "</script>\n";
    ?>
    but this doesn't
    Code:
    <script type="text/javascript">
    document.write("<?php echo \"Hello World\"; ?>");
    </script>
    I don't think the server parses .js files for php etc. Try making it a .php file and use the php header() function to set the content type to text/javascript. I think it's header("content-type: text/javascript");
    Last edited by FireLancer; 04-24-2008 at 10:41 AM.

  5. #5
    supajason's Avatar
    supajason is offline x10 Lieutenant supajason is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    England........:-)
    Posts
    288

    Re: include php within javascript

    you need to do this:

    add this to the main page:
    PHP Code:
    <script type="text/javascript" src="myjsfile.php"></script> 
    rename your js file to myjsfile.php, then in your new php(js) file you put:

    PHP Code:
    <?PHP
    Header
    ("content-type: application/x-javascript");
    $serverIP=$_SERVER['REMOTE_ADDR'];
    echo 
    "document.write(\"Your IP address is: <b>" $serverIP "</b>\")";
    ?>
    Last edited by supajason; 04-24-2008 at 10:30 AM.
    supajason was here

  6. #6
    thezone1 is offline x10 Sophmore thezone1 is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    192

    Re: include php within javascript

    Thanks for the help supajason, you out me on the right path, turn's out the the site runs off the index so i had to link to it there.

+ Reply to Thread

Similar Threads

  1. Replies: 3
    Last Post: 03-10-2008, 12:22 PM
  2. Sigo con problemas con phpbb2
    By reciecho in forum Soporte
    Replies: 7
    Last Post: 10-20-2007, 06:28 PM
  3. "PHP Startup: Invalid Library" - Interesting error
    By javaguy78 in forum Free Hosting
    Replies: 5
    Last Post: 03-27-2007, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers