1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to use the INCLUDE function on .htm pages?

Discussion in 'PHP' started by Mr.Dog, Apr 3, 2013.

  1. #1
    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...
    SEMrush
     
    Solved! View solution.
    Mr.Dog, Apr 3, 2013 IP
    SEMrush
  2. #2
    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.
     
    edduvs, Apr 3, 2013 IP
    Mr.Dog likes this.
  3. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    60
    #3
    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...
     
    Mr.Dog, Apr 3, 2013 IP
  4. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    110
    #4
    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.
     
    edduvs, Apr 3, 2013 IP
  5. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    60
    #5
    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.
     
    Mr.Dog, Apr 3, 2013 IP
  6. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    60
    #6
    ...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!)
     
    Mr.Dog, Apr 3, 2013 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    8,975
    Likes Received:
    1,635
    Best Answers:
    233
    Trophy Points:
    515
    #7
    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.
     
    deathshadow, Apr 3, 2013 IP
  8. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    60
    #8
    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?
     
    Mr.Dog, Apr 3, 2013 IP
  9. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    60
    #9
    ...not sure, maybe I should go for that Javascript, after all. There will be 2-3 boxes per page.
     
    Mr.Dog, Apr 3, 2013 IP
  10. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    110
    #10

     
    edduvs, Apr 3, 2013 IP
  11. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    60
    #11
    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.
     
    Last edited: Apr 3, 2013
    Mr.Dog, Apr 3, 2013 IP
  12. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    110
    #12
    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.
     
    edduvs, Apr 3, 2013 IP
  13. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    60
    #13
    I used the 1st one, the JQuery
     
    Mr.Dog, Apr 3, 2013 IP
  14. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    110
    #14
    Oh okay, then you must keep that <script> tag, there's no way you can substitute it with anything else.
     
    edduvs, Apr 3, 2013 IP
  15. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    60
    #15
    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?
     
    Mr.Dog, Apr 7, 2013 IP
  16. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    110
    #16
    Content from newsbox.php will inherit any CSS property that you're setting in the parent page.
     
    edduvs, Apr 7, 2013 IP
  17. Mr.Dog

    Mr.Dog Active Member

    Messages:
    912
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    60
    #17
    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!
     
    Mr.Dog, Apr 7, 2013 IP
  18. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    110
    #18
    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.
     
    edduvs, Apr 8, 2013 IP
  19. tasos55

    tasos55 Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #19
    Is your newsbox square ? if so you can put it in a iframe i think.
     
    tasos55, Apr 8, 2013 IP
  20. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    110
    #20
    If you would have paid more attention to the OP's posts here, you would have read that iFrame is not an option here.
     
    edduvs, Apr 8, 2013 IP