Results 1 to 10 of 10

Thread: embedded PHP in HTML not running.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    embedded PHP in HTML not running.

    I hope someone can help me with this as I cannot find an answer any where else.

    I have installed Apache 2, and php4 on my WinXP home machine.
    The problem I am getting is that when I embed php script in html pages the script is not getting run.
    So the following page will only display goodbye in the browser.

    PHP Code:
    <html>
     <head>
      <title>PHP Test</title>
     </head>
     <body>
     <?php echo "<p>Hello World</p>"?>
     goodbye
    </body>
    </html>
    The strange thing is if I navigate to a file with the php extension with php code in it works fine. This file was called http://localhost/hello.php
    PHP Code:
    <?php echo "hello world"  ?>
    Hello World is displayed in the browser.To setup PHP I followed these instructions
    When compared to the official PHP installation instructions I have done everything right. I think.


    Things I do when I am bored: DotNetable

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    LoadModule php4_module e:/php/sapi/php4apache2.dll
    AddType application/x-httpd-php .php

    -or-

    LoadModule php4_module e:/php/sapi/php4apache.dll
    AddModule mod_php4.c
    AddType application/x-httpd-php .php
    Did you make sure the LoadModule path is correct? Although I think you'd get an error.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    Yes I do, I have the former 2 line version.


    Things I do when I am bored: DotNetable

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: embedded PHP in HTML not running.

    Originally posted by davidrobin
    The strange thing is if I navigate to a file with the php extension with php code in it works fine.
    ok.. after reading that sentence, PLEASE, tell me you aren't trying to execute PHP in a file with the extension HTML. Unless you told Apache to recognize .html/.htm as your PHP extension, it isn't going to execute it. You absolutely HAVE to name it [name].php for Apache to recognize it and tell the PHP engine to parse it.
    Like Archer? Check out some Sterling Archer quotes.

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Re: embedded PHP in HTML not running.

    Originally posted by kows
    ok.. after reading that sentence, PLEASE, tell me you aren't trying to execute PHP in a file with the extension HTML. Unless you told Apache to recognize .html/.htm as your PHP extension, it isn't going to execute it. You absolutely HAVE to name it [name].php for Apache to recognize it and tell the PHP engine to parse it.
    Nice catch, kows. I thought he meant the problematic file was the .php file.

    You CAN setup your Apache to parse .html files...but anything you make wont work on any other server unless it's setup the same way, which isn't common.

    As kows said, for PHP code to work, it has to be a .php extension. You can't just plot PHP code in a .html file.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    I am trying to run a HTML page with the HTML extension that has PHP script embedded in it.

    The following file is named index.html

    PHP Code:
     <html> 
    <head> 
      <title>PHP Test</title> 
    </head> 
    <body> 
     <?php echo "<p>Hello World</p>"?> 
     goodbye 
    </body> 
    </html>


    Things I do when I am bored: DotNetable

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    OK,

    I have done a little more thinking and I think I understand.

    I know PHP files must have a PHP extension. The problem is in my understanding of PHP files was a file that has the start tag at the beginning and the end tag at the end and PHP code in the middle like so (I know they aren't tags as we know them in the HTML sence, but thats what I call them).
    PHP Code:
    <?php  some code 
    over many lines ?>
    Also, where I think I am going wrong is I thought my "hello world" example above was a html page because it started and ended with html tags, and that the php code was embedded. In the same way javascript is embedded, as I know you can use the <script language = "PHP"></script>tags just like javascript. This is probably what confused me.

    I have come to think a file/page with any php tags is a php page and must have a php extension and in my "hello world" example the html tags will just be treated as text and only the php will be "interpreted". If this is right then I will kick myself because this is how asp works (in theory).

    I also blame my confusion on the book I am reading "Teach yourself php 4 in 24 hours". I have just looked at it again and it does not say to save the files (e.g. hello world example) as php.

    *takes deep breath for next ramble*
    The conclusion is all files that contain PHP of any amount should have the php extension as kows said. And you cannot embed php in a html page as I initially thought.

    It all makes sence.

    Thanks guys.


    Things I do when I am bored: DotNetable

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Kick yourself, you got it right

    But you can enable PHP processing for whatever file type you like in your config:

    AddType application/x-httpd-php .xyz
    AddType application/x-httpd-php .html

    This makes .xyz and .html pages processed as PHP. This is not good though. It means that the PHP parser is invoked even for plain html pages without PHP, resulting in unnecessary overhead.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    yeah, its not something I think I would do.

    Another reason for my insanity is because I was trying this late at night rather than sleeping.

    Thanks Again........................


    Things I do when I am bored: DotNetable

  10. #10
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    keep in mind that it doesn't matter where your PHP starts in a PHP file.. your index.html can be renamed to index.php and will run fine. I'm saying this because while you tried to explain it, I thought you were thinking a PHP file had to start with the PHP tags (<? and ?>), while it actually doesn't.
    Like Archer? Check out some Sterling Archer quotes.

Posting Permissions

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



Click Here to Expand Forum to Full Width