View RSS Feed

molendijk

A Client Side Way of Getting PHP into HTML

Rate this Entry
This is pretty obvious, I suppose. I decided to post it anyway.
The standard way of getting PHP output into a HTML file (that you don't want to give the PHP extension) is to configure the server to interpret HTML files as PHP. But your web host may not support the htaccess file(s) needed for this. No worries, there's a client side way of getting PHP output into HTML. Just put the following lines where you want the output to display in your HTML file (and don't forget to reference the jQuery library in the head of your HTML document):
Code:
<div>
<iframe style="width: 0; height:0; opacity:0; " src="about:blank" onload="$(this.parentNode).load('your_php_file.php')"></iframe>
</div>
Alternatively, you could do (without jQuery):
Code:
<div>
<iframe style="width: 0; height:0; opacity:0; " src="your_php_file.php" name="phpfile" onload="this.parentNode.innerHTML=frames.phpfile.document.body.innerHTML"></iframe>
</div>
You must not conclude from this way of including a PHP file in a HTML document that there's no server side operation going on here. This becomes clear when the PHP document is a file that you allow your clients to edit online via some sort of Content Management System. Each edit they make to the PHP content will be immediately noticeable in the HTML document.

Demo here.

Submit "A Client Side Way of Getting PHP into HTML" to del.icio.us Submit "A Client Side Way of Getting PHP into HTML" to StumbleUpon Submit "A Client Side Way of Getting PHP into HTML" to Google Submit "A Client Side Way of Getting PHP into HTML" to Digg

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments