I'm building a webpage which has mostly static contents.Only one section of the page has to be generated based on the data from a text file.I wrote a PHP script to display that section.When I execute the script as a standalone script it displays the section correctly.Now I want to include this script in the html file .How do I do this?
I tried:
<?php
include "head.php"
?>
where the dynamic page was to be displayed in the HTML file.
But the page displays only the static contents.Is there any other way to execute the script from the html page?

Thanks

    what's the extension for this file? html? php?
    it must be php in order to execute the php code (in case the server doesn't parse the html files as php files)

      Just like malbera said, you would have to rename that file with a .php extension. 🙂

      Here's your file:

      ....
      ....
      html code
      ....
      ....

      <? include('head.php'); ?>
      ....
      ....
      more html code
      ....
      ....

        Alternatively, you could make the static html file into an SHTML file ( .shtml) and include your php content via:

        <!--#include virtual="head.php"-->

        although your server must have SSI (server side includes) enabled in addition to php for this to work.

          Thanks for the replies...
          The html file has .html extension.Maybe thats the reason its not working.I'll try to change it to .php.Hope it works this time....

            did you have the following code in your .html file?

            <?php
            include "head.php"
            ?>

            if so, do you have short tags turned on in you php.ini file? also, did you include a semicolon after the "head.php"?

            example: <? include ("head.php"); ?>

            currently, you are using XML style tags (<?php ... ?>)

            I don't think you can mix and match, but maybe I'm wrong and missing the point.

              Or... (if you're using Apache and have access to the server config file), you can edit the AddType line for PHP files to send .html and .htm files through the PHP interpreter.

                Perfectly true that you can edit httpd.conf to parse .html and .htm files for php, but a bit of overkill really and one that I as a server admin wouldn't do as it is putting unnecessary load on the server.
                I'd stick to either renaming the file to a parsed type, ie .shtml or .php etc

                  5 days later

                  Thanks everyone!!!!!!:-)
                  I renamed the file as .shtml and the script worked....

                    4 years later
                    mrmufin wrote:

                    Or... (if you're using Apache and have access to the server config file), you can edit the AddType line for PHP files to send .html and .htm files through the PHP interpreter.

                    What should be changed/added in httpd.conf file? And do i have to do anything on my page too?

                      As was mentioned, it is really not wise to run all .html/.htm files through the PHP parser. Why can't you just rename the file to .php ?

                        a month later

                        I have a php page that has a form to upload files on my site. it uploads to /upload/popular/
                        after upload another php comes up with the statis information (filename and bytes of file) or if it didnt upload it has an error message. how can I get one of these php files to run the resize.php file. I tried both <? include ("head.php"); ?>
                        and <!--#include virtual="head.php"--> putting each as you see them in at the end of the file.
                        Heres the files Im trying I get this error Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /path/radupload/resize.php on line 8

                        upload1.php

                        <html>
                        <head><title>popular</title></head>

                        <body bgcolor="FFFFCC" style="margin: 1px">
                        <table border="1" cellpadding="5" width="100%" align="center">
                        <tr><td colspan="2" bgcolor="#0066cc"><font color="#FFFFCC" size="+1" align="center">Files Uploaded</font></td></tr>
                        <tr bgcolor="#ffff00"><td><nobr>File Name</nobr></td>
                        <td align="right"><nobr>File size</nobr></td></tr>
                        <?

                        /
                        SET THE SAVE PATH by editing the line below. Make sure that the path
                        name ends with the correct file system path separator ('/' in linux and
                        '\' in windows servers (eg "c:\temp\uploads\" )
                        */

                        $save_path="/path/uploads/popular/";

                        $file = $_FILES['userfile'];
                        $k = count($file['name']);

                        for($i=0 ; $i < $k ; $i++)
                        {
                        if($i %2)
                        {
                        echo '<tr bgcolor="#FFFF99"> ';
                        }
                        else
                        {
                        echo '<tr>';
                        }

                        echo '<td align="left">' . $file['name'][$i] ."</td>\n";
                        echo '<td align="right">' . $file['size'][$i] ."</td></tr>\n";
                        
                        if(isset($save_path) && $save_path!="")
                        {
                        	$name = split('/',$file['name'][$i]);
                        
                        	move_uploaded_file($file['tmp_name'][$i], $save_path . $name[count($name)-1]);
                        }

                        }

                        echo "<tr style='color: #0066cc'><td>SSL</td><td>". (($_SERVER[HTTPS] != 'on') ? 'Off' : 'On') ."</td></tr>";
                        if(! isset($save_path) || $save_path =="")
                        {
                        echo '<tr style="color: #0066cc"><td colspan=2 align="left">Files have not been saved, please edit upload.php to match your configuration</td></tr>';
                        }

                        ?>
                        </table>
                        <? include ("resize.php"); ?>
                        <p>&nbsp;</p>
                        <a href="http://kansasenterprises.com/hangarhobbies/">Back to WebSite</a>
                        </body>
                        </html>

                        resize.php

                        <?php
                        // Max height and width
                        $max_width = 177;
                        $max_height = 230;

                        // Path to your jpeg

                        $upfile '/uploads/popular/';
                        Header("Content-type: image/jpeg");

                        $size = GetImageSize($upfile); // Read the size
                        $width = $size[0];
                        $height = $size[1];

                             // Proportionally resize the image to the
                             // max sizes specified above
                        
                             $x_ratio = $max_width / $width;
                             $y_ratio = $max_height / $height;
                        
                             if( ($width <= $max_width) && ($height <= $max_height) )
                             {
                                   $tn_width = $width;
                                   $tn_height = $height;
                             }
                             elseif (($x_ratio * $height) < $max_height)
                             {
                                   $tn_height = ceil($x_ratio * $height);
                                   $tn_width = $max_width;
                             }
                             else
                             {
                                   $tn_width = ceil($y_ratio * $width);
                                   $tn_height = $max_height;
                             }
                         // Increase memory limit to support larger files
                        
                         ini_set('memory_limit', '32M');
                        
                         // Create the new image!
                         $src = ImageCreateFromJpeg($upfile);
                         $dst = ImageCreateTrueColor($tn_width, $tn_height);
                         ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
                         ImageJpeg($dst);

                        // Destroy the images
                        ImageDestroy($src);
                        ImageDestroy($dst);
                        ?>

                          3 years later

                          Am presently designing a mobile site,but my php script does not execute,its shows me syntax error,e.t.c.pls i nid help..

                            Danvc;10958342 wrote:

                            Am presently designing a mobile site,but my php script does not execute,its shows me syntax error,e.t.c.pls i nid help..

                            Three things you'll want to do:

                            1. Don't hijack someone else's thread.

                            2. Don't post in the wrong thread; there is a stickied thread in the Coding forum entitled Parse Error: syntax error dedicated for such errors.

                            3. Don't expect us to read your mind; if you have a parse error, post the exact error message as well as a few lines of code centered around the line number given in the error message (don't forget to indicate exactly which line the error message referenced).

                              Write a Reply...