// cart.php - this file handles the user's cart session_start(); // get the latest cart items $cart_items = get_latest_cart_items(); // return the updated cart items as JSON echo json_encode($cart_items);
// script.js - this script updates the cart using AJAX // define the URL to the cart.php file var cart_url = "cart.php"; // update the cart every 5 seconds setInterval(function() { $.ajax({ url: cart_url, success: function(data) { // update the cart with the latest items update_cart(data); } }); }, 5000);In this example, the `cart.php` file retrieves the latest cart items and returns them as JSON using `json_encode()`. The `script.js` file updates the cart every 5 seconds using AJAX and the `update_cart()` function. The package library used in this example is jQuery.