Just wondering is this possible? I am trying to do a mini announcement section in my website. What i want is for the announcement to be taken from a txt file for easy updating.

Regards,
Jon

Dani AI

Generated

As asked about keeping announcements in a .txt for easy updates and touched on the limitations of static pages, here are practical, current ways to do it and the important caveats.

Server-side (recommended): have the server read the file and inject it into the page. Example (place in a .php page):

<?php
// output safely: escape any HTML and preserve newlines
echo nl2br(htmlspecialchars(file_get_contents(__DIR__ . '/announcements.txt')));
?>

Client-side (if you cannot run server code): fetch the text over HTTP and insert it as plain text to avoid XSS:

fetch('/announcements.txt')
  .then(r => r.ok ? r.text() : Promise.reject(r.statusText))
  .then(t => document.getElementById('announcements').textContent = t)
  .catch(e => console.error('Announcement load failed:', e));

Notes and troubleshooting:

  • If using fetch, the file must be served over HTTP from the same origin or allow CORS; see the Fetch API.
  • File permissions and webserver config must let the server read and serve the file.
  • Treat the .txt as untrusted input: use textContent or escape output on the server (htmlspecialchars) to prevent script injection.
  • For structured announcements (timestamp, author), use JSON instead of plain text and parse it on the client or server.
  • PHP reference for reading files: file_get_contents.

Recommended Answers

All 2 Replies

This is not possible with HTML or CSS. You would need a server-side language like PHP.

Regards
Arkinder

Thanks for the info.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.