Hello guys, im a PHP system programmer. My javascript/ajax skills are very limited.
What i need to do is updating a div tag whenever an XML file updates.
And if you cant help me with that i can do with just updating a div tag with PHP script inside every now-andthen.
I just need a direction with link or so, i dont expect a ready-made code.

Regards,
Sorcher

Dani AI

Generated

— you already got the right high-level advice from . For a PHP developer with limited JavaScript the fastest, most reliable path is to keep the server-side logic in PHP and use a lightweight client poll that only downloads the XML when it actually changed. That avoids complex setups (Comet/long-poll) which are often fragile on shared hosting.

A practical, low-effort server pattern:

  • Have a tiny PHP endpoint that checks the XML file timestamp and returns a 304 Not Modified when unchanged. Use filemtime() to detect changes and send Last-Modified (or ETag) headers so the browser/client can make conditional requests. See PHP docs for filemtime and the browser/server conditional-request behavior on MDN: filemtime (PHP) and Conditional requests (MDN).

Example server logic (minimal):

$xml = 'data.xml';
$mod = filemtime($xml);
$if = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) : 0;
if ($if >= $mod) { header('HTTP/1.1 304 Not Modified'); exit; }
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $mod).' GMT');
readfile($xml);

Client side: use a simple fetch/XHR loop at a reasonable interval (start 5–15s) and update the DIV only when content changed. This keeps the JS small and leverages existing PHP skills.

If true real-time push is required later, consider Server-Sent Events (simple unidirectional push) or WebSockets (full duplex). See MDN on SSE and WebSocket for browser APIs and the Ratchet project if you want a PHP WebSocket server: Using server-sent events (MDN), WebSocket (MDN), Ratchet (GitHub).

Start with the conditional-polling approach — quick to implement, low server load, and friendly to shared hosts.

Sorcher,

First, you need to use AJAX to fetch the data from the server. Easiest way to do AJAX is to use the jQuery lib, which has AJAX methods built in.

Next you have to decide how to employ AJAX to get updates. There are two possible approaches:

Polling: (simple) Make regular AJAX requests. You will need to use javascript's setInterval() or setTimeout().

Long-polling: (difficult) Make a single AJAX request and arrange for the server to make multiple responses without completing the HTTP response. I have never tried this but understand it to be very difficult (impossible?) under Apache. The experts talk about something called Comet but you would need control of your server to install it - not generally possible with cheap/shared web hosting.

Unless you know what you're doing with long-polling/comet, then use the first approach, polling, with setTimeout(makeRequest, x); in the AJAX success handler to initiate another request to your js makeRequest function in x milliseconds time.

To get you started, try reading the jQuery.ajax() entry in the API. The full range of settings is daunting; you should need url, type, data, cache, dataType, success, error and complete.

If you are prepared to try long-polling, then you will need to use the statusCode option. I'm about to try this myself for the first time in a current project.

Airshow

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.