Hi, I want to create a PHP "newsbox" that will display on all of my .htm pages. For this I am going to use this formula on my .htm pages: <div width="300px" height="300px"> <?php include("newsbox.php"); ?> </div> Code (markup): Of course, I have the external newsbox.php source file which displays, but only if my host page is .php! How can I use the INCLUDE function on .htm pages to load code from the newsbox.php file? It's important, because I SEO-ed for those .htm pages and I want to keep them .htm. So far I could only make the INCLUDE work if I change the host page's extension to .php...
You can always make an XHR request (JavaScript) or just do a simple AJAX call if you have jQuery in your pages already. jQuery version : <script type="text/javascript"> $(document).ready(function(){ $.ajax({ url : 'newsbox.php', success : function(content) { $('#news').html(content); // dont forget to add id="news" on that div } }); }); </script> PHP: XHR version | add it before </body> ends : (if you don't have jQuery included and you don't want it there) <script type="text/javascript"> var xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", "newsbox.php",true); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState == 4) { document.getElementById('news').innerHTML = xmlhttp.responseText; } } xmlhttp.send(null) </script> PHP: Remember that using XHR instead jQuery library will get you headaches on older browsers. Thats why I recommend using jQuery with a simple line in the <head> tag , before the script that I wrote for your earlier: <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> PHP: Regards.
Gosh... isn't there a simpler way to do this? I want to minimize scripts that load from external sources - for the sake of speeding the page up. At first I wanted to do this with an iFrame, but that's a bit laggy and not as sophisticated as a PHP code. But INCLUDE won't load from a .htm page. Javascript slows the page down... Thanks for helping, but isn't there a simpler solution? It's a very simple site, really...
How does javascript slow the page down ? Google, Yahoo, Bing, Facebook, Microsoft, YouTube, they are ALL using jQuery. I guess there is 1 of 5 users whom browsers don't have jquery.min.js already cached in their memory. There are no simple ways to accomplish this, just the RIGHT way or the GOOD way, which in my opinion, is NOT enough.
Anything you put on a page slows it down a bit: images, codes, etc. etc. and the Javascript is in fact one of the main reasons for slower loading times... I will have lots of images, so I want to speed the page up. I am trying to load that newsbox.php with as little code as possible, as few requests as possible - without renaming my pages to .php.
...in case I chose the Javascript version: why should I have to add that "news" thing to a DIV; which DIV? I'm a bit confused here... (I'm an online marketer, not a coder, by the way!)
If you're worried about it from a SEO standpoint, make those .htm files be 301 redirects. If you REALLY want PHP to parse .htm files, you could add this to your .htaccess (assuming an apache host) [codeAddType application/x-httpd-php .htm .html AddHandler x-httpd-php .htm .html[/code] Most servers only need one line or the other from that, some take both... there's no harm in including both. Just beware that this makes PHP parse ALL .htm and .html files. Though really, I'd just make them all .php unless you REALLY have a lot of backlinks. Shouldn't take more than a week for any search mojo to transfer unless you've bought into some sort of black asshat SEO scam nonsense... which your being a "online marketer" means that's quite possibly the case. Several years ago I came to the conclusion that if you have content of value people want marked up semantically for the visitor, any penalty from renaming/moving a file is for all intents and purposes nonexistent after a week or two.
Tried that .htaccess code, but it didn't work - the "newsbox" isn't loading: AddType application/x-httpd-php .htm .html AddHandler x-httpd-php .htm .html By the way - the htaccess file was empty... is that normal?
Oh OK... And: within my newsbox.php I intend to have another DIV setting with CSS for the content of that box. Not sure if CSS can be loaded by .php pages. By the way - if worked with the Javascript Thanks, mate! Is there any alternative for not having this in the heading: (?) <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> I would prefer it towards the end of the code... not sure if that can be done. Because of speed issues: what's at the beginning slows down the page... a bit.
Which code have you copied, the one with the XHR or the one with jQuery ? If it's the one with the XHR that you copied, the <script> tag in the header is not needed.
Oh okay, then you must keep that <script> tag, there's no way you can substitute it with anything else.
Hey mate, I have another issue, as I'm unable to apply CSS settings on a PHP page to the DIV that will be included on the host page... I know that CSS is not quite easy to use with PHP. Are you available - may I send you a PM?
A "bug" came in... solved it. It was more about the HTML than PHP. Solved it! Too bad we can't load CSS in PHP like we can in HTML!
Well, everything you're loading in the HTML parent page will automatically inherit the stylesheet. You just have to wrap that content in some selectors (divs, classes etc.). Regards.
If you would have paid more attention to the OP's posts here, you would have read that iFrame is not an option here.