Hello guys,

I just discovered these amazing forums. I am sure you guys will be able to help me out in this small problem/query that I have. 🙂

Like we can implement cgi scripts or javascripts in .html pages, similarly can we use php scripts in static html pages. I do not want to change the server setting so that it considers each html page as php and then parse the script.

What I was seeking for was retaining my html pages and including in them a php script which calls data from a mysql database and throws the data in the html file. I know that such a thing is possible using IFrames, but I do not wish to use them in the middle of my pages.

Is there a way in which this can be done?

Thanks in advance,

IM

    the .php pages must be parsed by the php engine. there is no way to make it work differently. any page that has a php script needs to have the .php extention, similar to .asp pages...

    php is also server side code and must be processed at the server, javascript as you refer to it would seem to be clientside code and client canhandle it as the user has choosen (including not to handle it at all).

    hth

      if your server is Apache, then you just need to and .htm and .html to the Addtype application/x-httpd-php section of the httpd.conf file.

      Addtype application/x-httpd-php .html
      Addtype application/x-httpd-php .htm

      and reboot apache.

      This is fine if it's your server, if not ask for administrator if he could do it. The downside is that all htm pages wil be parsed now and use more system resources.

        Hi Guys,

        Thanks for the quick response! Would this manipulation in the apache config affect the speed with which the page gets loaded. Plus are .php pages search engine friendly?

        Basically what do you guys suggest. Is it a feasible option? What are the pros and cons of this stuff.

        IM

          Also, what is the approx percentage increase in the consumption of the resources? Sorry for asking so many questions. 🙂

            Making your server parse html files as .php files would be stupid and a waste of resources no matter how big or small IMO. How many pages are you planning on 'converting' to this new page? If it is only a few, you might as well just make them the .php extension. Like one of the other posts said, you cannot get around the fact that you need to parse php.

              There are about 6000 pages. I guess I wouldn't go in for changing them to php. That would be a waste of lot of resources. 🙁

              Currently, I have implemented an Iframe on the html page and am calling the php script inside this Iframe, by using the folowing code.

              <iframe src="http://www.domain.com/myscript.php" width="100%" height="100%" marginwidth="0" marginheight="0 frameborder="0"></iframe>

              My main motto is to call some data using the php script and I am able to do that with Iframe. A disadvantage of this Iframe is that it looks pathetic on the page. What I want is that the Iframe should automatically adjust its height in acordance to the data that is being called inside. Neither scrolling=NO, scrolling=Yes, or scrolling=Auto, does that work. Any Iframe experts out here.

              Or another option would be to go for a cgi script to call that data. That way I wouldn't have to do the apache config, and would thus be saving a large amount of resources. But again, the disadvantage of using cgi script is the loading speed.

              Any input or suggestions

              IM

                3 years later

                it's easy
                in your HTML:
                <script type='text/javascript' src='my.php'></script>

                in your PHP:
                echo "alert('Hello from the PHP file!')";
                OR
                echo "document.write("Hello from the PHP file!")";

                you can compile any amount of content to be echoed back as a document.write or whatever...

                  While the amount of resources used by adding .php to the apache conf is by no means trivial, in my experience it doesnt add much load at all. And unless you are already strapped for resources, it shouldnt really be a big issue.

                    you could use php to not only display your website but save you space use mysql to store each pages content. stuff such as menu's etc can be a part of the template file CMS's like this are easy to make and even easier to maintain because with php you can just edit the mysql data without worrying about the general layout of the website such as with HTML. this setup could run on a 166MHz 24MB pc trust me I used to test my scripts server load against my own scalethis means that with most webhosts that add php for nothing because it costs nothing you can save space in storage which is one of the most expensive things when considering a webhost. you would still use about the same bandwidth because the outputed files would still be the same size bottomline php is great for most websites 😃

                      the question was [basically] "How do I include PHP scripts in HTML files WITHOUT using iframes?".

                      to answer that you should just replace them with PHP files instead answers nothing...

                      here is a way to get PHP in your HTML if you:
                      1. do not have access to your conf file to set HTML files to be parsed as PHP
                      2. do not want to parse HTML files as PHP files
                      3. cannot replace HTML files with PHP files (prohibitive time/cost/etc.)

                      try this: in your HTML code call for a PHP file as the javascript source. in the PHP file, compile the HTML content that you want and echo to the browser a document.write().

                      you need 2 files to try this (test.html and test.php)
                      test.html ->
                      <html>
                      <head>
                      <title>PHP in HTML</title>
                      </head>
                      <body>
                      <script type='text/javascript' src='test.php'><script>
                      </body>
                      </html>

                      test.php ->
                      <?php

                      $query = "SELECT * FROM table_name"; //REPLACE WITH YOUR OWN MYSQL QUERY
                      $data = mysql_query($query);
                      while ($result = mysql_fetch_array($data))
                      {
                      $output .= "<b>$result[example1]</b>: $result[example2]<br />"; //REPLACE WITH YOUR OWN HTML COMPILATION
                      }

                      echo "document.write(\"".addslashes(stripslashes(str_replace("\r\n",null,$output)))."\")";

                      ?>

                      NOTES:

                      you have to watch out for new lines inside the $output compilation using the keyboard. it will result in a javascript error.

                      also, you must be careful with quotes in $output. that's why i use the "addslashes(stripslashes($output))" (strip first so the content is clean, then add the slashes).

                        Does the above mentioned code work. I only ask because I've never thought of doing this before dynamic javascripts. it would save a tonne of bandwidth with those mamoth DHTML projects that have like 113kb .js files I like this idea superb

                          Write a Reply...