JDT |
FREE Downloads |
|
Embed a PHP Script in an HTML (.htm or .html) Web Page |
|||
|
If you want to include some PHP code on a web page, you need to give the page a .php file extension. This ensures that the web server knows that the page needs to be processed by the PHP pre-processor - the result(s) of which can then be sent to the browser. If you include PHP code in a normal HTML web page - one that has an extension of .htm or .html - the web server will not know that the PHP code needs to be pre-processed before the web page is sent to the browser. The result of this is that the web server will try to process the PHP code as HTML code, which will not work! What will normally happen in this situation is that the PHP code itself will be displayed in web page. What I have found from time-to-time though, is that I have a normal HTML page, that maybe has been part of a web site for quite a while, onto which I want to put some PHP code. This was the case with he web page that describes how to use Earnings Tracker. After this web page had been in existence for about six months or so, I decided that I wanted to display how many people had registered to use the Earnings Tracker accounting tool. This required writing a bit of PHP code to access a MySQL database. I could have renamed the web page from earningstracker_use.htm to earningstracker_use.php, but that would have meant changing all the hyperlinks that linked to that page - some of which I didn't control myself. So, I decided to create a new PHP web page that contained the code to access and present the data from the database, and then to embed the results from that page into my HTML web page. Here is the code I used to do this: <iframe src="php/users.php" width="400" height="23" frameborder="0" marginwidth="0" marginheight="6" scrolling="no"></iframe> As you can see I've used an iframe to embed the results of the PHP file (users.php) into the HTML web page. Now I know that some people frown upon the use of iframes, and frames in general, but in this situation they provide a very simple solution to the problem. Something to note when using iframesA few years ago I did a bit of maintenance work on a static web site. The web site had been around for quite a while but the owner of the site wanted to include some database listings on some of the pages. The problem was though, that the database was held on a different web server to that of the web site. I decided to use the iframe approach. What I didn't know though was that you cannot dynamically resize an iframe if its contents are pulled in from a different web server to that on which the web page containing the iframe is hosted. This was a problem because the database listings varied in length, and if the iframe was not high (long) enough to display all the listing(s), scroll bars were inserted in the iframe, which looked awful. What I had to do in the end was to make the height of the iframe much greater than the database listings. This meant that there was a bit of white space beneath the iframe on the web page, but I figured that that was better than scroll bars. If you are pulling content into an iframe, where the calling web page and the content are on the same web server, you can use a bit of JavaScript to dynamically resize the height of the iframe depending on how much content there is. Go back to PHP Tutorials home page Go back to Tutorials home page
|
|