PDA

View Full Version : Display information from a text file in a HTML document




Niwa Rose
Dec 7, 2009, 02:43 PM
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.



miles01110
Dec 7, 2009, 02:53 PM
Copy/paste it in between <html></html> tags? Could you describe your problem in more detail?

angelwatt
Dec 7, 2009, 02:55 PM
<?php
include 'file.txt';
?>
Server Side Includes (SSI)
<!--#include virtual="file.txt"-->

If you need further help, show what you've tried.

Niwa Rose
Dec 7, 2009, 02:55 PM
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.

Niwa Rose
Dec 7, 2009, 02:56 PM
<?php
include 'file.txt';
?>Server Side Includes (SSI)
<!--#include virtual="file.txt"-->If you need further help, show what you've tried.
I know where to put the PHP code, where do you put the commented code though?

angelwatt
Dec 7, 2009, 02:58 PM
I know where to put the PHP code, where do you put the commented code though?

Server Side Includes (http://en.wikipedia.org/wiki/Server_Side_Includes)

Niwa Rose
Dec 7, 2009, 03:03 PM
Server Side Includes (http://en.wikipedia.org/wiki/Server_Side_Includes)

^^ So I just place it in the web document? The code I have so far is...


<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?

angelwatt
Dec 7, 2009, 03:05 PM
It is saved as a html file. Another question, would I need it to be on a server?

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.

Niwa Rose
Dec 7, 2009, 03:06 PM
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.

That's probably why it hasn't been able to work. Is there a way I can accomplish the same thing using JavaScript?

angelwatt
Dec 7, 2009, 03:11 PM
That's probably why it hasn't been able to work. Is there a way I can accomplish the same thing using JavaScript?

You'd have to use AJAX (a JavaScript technique), but it's not as straight forward. Here's a tutorial to get you started. (http://www.openhosting.co.uk/articles/webdev/5899/) You should look into installing MAMP (http://www.mamp.info/en/index.html) so you can test server stuff locally though as it's much easier.

Niwa Rose
Dec 7, 2009, 03:13 PM
You'd have to use AJAX (a JavaScript technique), but it's not as straight forward. Here's a tutorial to get you started. (http://www.openhosting.co.uk/articles/webdev/5899/) You should look into installing MAMP (http://www.mamp.info/en/index.html) so you can test server stuff locally though as it's much easier.

I already have WAMP install so I should be able to just use that. ^^ I'll look into it, thanks.

rickiac
Dec 7, 2009, 03:36 PM
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.

JohnDoe8450
Dec 7, 2009, 04:29 PM
Seriously though, PHP includes are definitely the way to go. If you've got MAMP installed the following code should do juste fine...


<html>
<head>

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

</body>
</html>

Niwa Rose
Dec 8, 2009, 01:40 PM
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