3

I have an HTML5 page (index.html), and I need to display the contents of a .txt file (p001wide.txt). Both are on the server in the same directory.

The text file is generated from a CMS, so I cannot change the format of that file.

In the .txt file is a variable named widetxt. I need to display the contents of the widetxt variable on the page.

What do I need to do to parse the the textfile and display it in the HTML page?

Do I need to use javascript, and if so, how?

Livi17
  • 1,620
  • 3
  • 25
  • 43
  • What does the text file look like so people how it can be parsed? You will be using Ajax to fetch the file. – epascarello Mar 13 '12 at 00:56
  • textfile simply looks like this: `widetxt=This is the text contained in the textfile&done=1` I'll use whatever I can to fetch the file. – Livi17 Mar 13 '12 at 01:13

3 Answers3

1

As both your index file and the text file you wish to read reside on the server you should be able to read the text file on the server and insert what you wish from it into the index file using PHP.

See http://www.w3schools.com/php/php_file.asp for a tutorial on how that can be done without resorting to client side script.

If you cannot alter your index file on the server and can put a PHP file on the server you can use AJAX to ask your PHP file to read the contents of the text file and return it to you. Then you would use javascript to insert it as you wish. I presume that you can alter the index file because otherwise you could not alter its javascript either.

JimFuqua
  • 961
  • 1
  • 7
  • 12
1

Hm, I found this question which appears to be similar to yours as far as reading a file goes. As for parsing though why not use a database such as MySQL to store your data? This way you can quickly add and query through your data.

Community
  • 1
  • 1
Adam Ashwal
  • 1,472
  • 1
  • 19
  • 36
  • I don't have the option... its a very old obsolete system, and the data is stored in text files. There isn't even a database. Secondly, this has to run from a CD without an internet connection, which is partly the reason why data is stored in text files. – Livi17 Mar 13 '12 at 01:11
0

You can do this with JavaScript, you basically need to learn how to read and write files (and you mention the variable inside, this would be parsing). Start reading here - http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm other information, just Google read write files javascript, or parsing text with javascript.

Best of luck!

Ryan Kempt
  • 4,200
  • 6
  • 30
  • 41