Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Emman124's avatar

table reload

Help, How to reload a specific table using ajax, im currently using this location.reload();

0 likes
1 reply
Tray2's avatar

You can use the fetch API to fetch the new data from the server, then parse it into a table body and replace the table body.

async function getRequest(url, parameters = {}) {
  try {
      let responsePromise = await fetch(url, { method: 'GET', headers: parameters});
      if(! responsePromise.ok) {
          throw new Error(responsePromise.status );
      }
      return await responsePromise.json();
  } catch (error) {
      return error;
  }
}
 

async function refreshTable() {
	let response = getRequest('/api/v1/table-api');
    let tbody = '';
   
    response.items.forEach((item) => {
		tbody += `<tr><td>${item.field1}</td><td>${item.field2}</td></tr>`;
    });

    document.querySelector('#table-body').innerHTML = tbody;
}

Please or to participate in this conversation.