Refresh a div with php data every 10 seconds using jQuery

Refresh a div with php data every 10 seconds using jQuery

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Selector Trap in jQuery .load()

The core issue preventing your "test" string from appearing lies in a subtle but critical syntax error within your jQuery selector. In your line:

$("#latestData").load("getLatestData.php #latestData");

You are attempting to load the contents of getLatestData.php, but you have inadvertently appended a second CSS selector (#latestData) to the URL string itself. jQuery interprets the entire string "getLatestData.php #latestData" as the path to the file to fetch. Since your web server likely cannot find a file literally named getLatestData.php #latestData, the request fails silently, and no data is injected into the DOM.

To fix this, you must separate the target URL from the target element selector. The .load() method signature expects two distinct arguments: the source URL and optionally the selector within that source file to load. If you do not want to limit the load to a specific part of the response, pass an empty string or omit the selector argument entirely.

The corrected code should look like this:

$("#latestData").load("getLatestData.php");

Or, if getLatestData.php outputs HTML that contains a specific ID you want to isolate (which is generally not recommended for data-only endpoints), you would use:

$("#latestData").load("getLatestData.php", "#targetId");

However, in your case, since getLatestData.php echoes raw text (echo "test";), you should load the entire response into the element. The fix is simply removing the space and the ID from the URL string.

Handling Cross-Origin Requests

While fixing the selector solves the immediate problem, there is a significant architectural limitation you need to consider regarding how your PHP backend communicates with your frontend. You are loading getLatestData.php from a separate file. If getLatestData.php resides on the same domain as your main HTML page, browsers will handle this via standard HTTP requests without issue.

However, if you ever decide to move this logic to a different subdomain (e.g., api.example.com) or a Content Delivery Network (CDN), the browser's Same-Origin Policy will block the .load() method from reading the response body unless you explicitly configure your server headers. You must ensure that getLatestData.php sends the header Access-Control-Allow-Origin: *. Without this header, modern browsers will throw a CORS error, and your div will remain empty regardless of the PHP output.

Best Practices for Long-Polling Data

Using .load() every 10 seconds is functionally equivalent to "long-polling" or simple polling. While this approach works for static data updates like weather widgets or ticker prices, it introduces unnecessary overhead for high-frequency applications. Every 10 seconds, your browser initiates a new HTTP request, waits for the server to process it, and then renders the update.

For more efficient real-time updates where latency matters, consider switching to WebSockets or Server-Sent Events (SSE). These technologies allow the server to push data to the client instantly when an event occurs, rather than waiting for the client to ask again. If you are building a Laravel application, the built-in support for these protocols can simplify the backend logic significantly compared to managing manual polling intervals with setInterval.

Conclusion

By correcting the selector syntax in your jQuery .load() call and ensuring proper CORS headers on your PHP endpoint, your div should begin refreshing correctly. Keep in mind that while polling is a viable solution for simple data refreshes, architectural shifts toward push-based technologies often yield better performance as your application scales.

Our network