Display information from a text file in a HTML document

Discussion in 'Web Design and Development' started by Niwa Rose, Dec 7, 2009.

  1. Niwa Rose macrumors newbie

    Niwa Rose

    Joined:
    Oct 17, 2008
    Location:
    Utah
    #1
    Okay all, I've come across a difficult situation. I want to take a text file that has all the information on a page (not HTML code) and have it display on a Web Page. I've tried tutorials using PHP and JavaScript, but none of them have worked for me. Help is much appreciated.
     
  2. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #2
    Copy/paste it in between <html></html> tags? Could you describe your problem in more detail?
     
  3. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #3
    PHP:
    <?php
    include 'file.txt';
    ?>
    Server Side Includes (SSI)
    Code:
    <!--#include virtual="file.txt"-->
    If you need further help, show what you've tried.
     
  4. Niwa Rose thread starter macrumors newbie

    Niwa Rose

    Joined:
    Oct 17, 2008
    Location:
    Utah
    #4
    I want information in a text file to show up in a Web page. For example, I can just edit the text information and it will change the text on the Web page, without dealing with the XHTML code. It's called a PHP include for the php solution.
     
  5. Niwa Rose thread starter macrumors newbie

    Niwa Rose

    Joined:
    Oct 17, 2008
    Location:
    Utah
    #5
    I know where to put the PHP code, where do you put the commented code though?
     
  6. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #6
    Server Side Includes
     
  7. Niwa Rose thread starter macrumors newbie

    Niwa Rose

    Joined:
    Oct 17, 2008
    Location:
    Utah
    #7
    ^^ So I just place it in the web document? The code I have so far is...

    Code:
    <html>
    <head>
    
    </head>
    <title>test</title>
    <body>
    <?php
    include 'testfile.txt';
    ?> 
    <!--#include virtual="testfile.txt"-->
    
    </body>
    </html> 
    It is saved as a html file. Another question, would I need it to be on a server?
     
  8. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #8
    If the file has a .html extension, then the web server needs to be configured to parse .html files for PHP. Also, the server needs to be setup to use SSI for .html files (some do by default). You'll need to run it on a server for both the PHP and SSI to work as they are both server-side languages.
     
  9. Niwa Rose thread starter macrumors newbie

    Niwa Rose

    Joined:
    Oct 17, 2008
    Location:
    Utah
    #9
    That's probably why it hasn't been able to work. Is there a way I can accomplish the same thing using JavaScript?
     
  10. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #10
    You'd have to use AJAX (a JavaScript technique), but it's not as straight forward. Here's a tutorial to get you started. You should look into installing MAMP so you can test server stuff locally though as it's much easier.
     
  11. Niwa Rose thread starter macrumors newbie

    Niwa Rose

    Joined:
    Oct 17, 2008
    Location:
    Utah
    #11
    I already have WAMP install so I should be able to just use that. ^^ I'll look into it, thanks.
     
  12. rickiac macrumors newbie

    Joined:
    Dec 10, 2007
    #12
    Try changing the file extension

    When using the SSI, try using .shtml instead of .html. This has worked for me in the past. I have used a text file as a footer multiple pages, when I needed to make a change I just updated the .txt file.
     
  13. JohnDoe8450 macrumors member

    Joined:
    Feb 3, 2009
    #13
    Seriously though, PHP includes are definitely the way to go. If you've got MAMP installed the following code should do juste fine...

    PHP:
    <html>
    <head>

    </head>
    <title>test</title>
    <body>
    <?php
    include 'testfile.txt';
    ?>

    </body>
    </html>
     
  14. Niwa Rose thread starter macrumors newbie

    Niwa Rose

    Joined:
    Oct 17, 2008
    Location:
    Utah
    #14
    Thanks all, I finally got it to work. The probably was I wasn't testing it on a server and that I didn't rename my html file as a php file. xD
     

Share This Page