Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
My async fetch function takes me to the endpoint I POST to. I want it to refresh the page it POSTS from

My async fetch function is embedded on names.php and when I activate the function it takes me to the endpoint it POSTS to: change-name.php. I want to remain on the names.php page but refresh that page after the fetch function has run.

I am clicking the span below to open a text field where I add a name and click the button:

<span onclick="addGuestName(this)">

<button class="addPaxName btn btn-xs btn-warning">ADD</button>

This works perfectly fine. And updates the name but I am transported to the /change-name.php page after clicking the ADD button.

My desired response is to remain on the original page with a page refresh after the fetch function has completed. How do I get this result.

async function updateGuestName(paxid,name){
paxIDbody = '{"pxid": "' + paxid + '", "name": "' + name + '"}';
  console.log("PaxID:", paxIDbody);
  try {
    const settings = {
      method: "POST",
      headers: {
        "Content-type": "application/json; charset=UTF-8"
      },
      body: paxIDbody,
    };
    const response = await fetch(
      "/change-name.php",
      settings
    );
    const data = await response.json();
    console.log("DATA: ",data);
  } catch (error) {
    console.log("ERROR: ",error);
  }
}

function addGuestName(obj) {
  const itemClicked = obj;
  const paxid = obj.id;
  const addPaxName = itemClicked.nextElementSibling;
  const addPaxNameButton = itemClicked.nextElementSibling;
  addPaxNameButton.style.display = 'inline-block';
  var addPaxNameField = document.createElement('input');
  addPaxNameField.setAttribute('type', 'text');
  addPaxNameField.setAttribute('name', 'visitorNameSurname[]');
  addPaxNameField.setAttribute('placeholder', 'Enter Name & Surname');
  itemClicked.parentNode.insertBefore(addPaxNameField, itemClicked.nextSibling);
  addPaxNameField.setAttribute("required", "required");
  addPaxNameButton.addEventListener('click', () => {
    const name = addPaxNameField.value;
    updateGuestName(paxid,name);
  })
}

My PHP endpoint returns JSON after the DB update:

header('Content-type:application/json;charset=utf-8');
    $myObj=new \stdClass();
    $myObj->status = $status;
    $myObj->message = $message;
    $myJSON = json_encode($myObj);
    echo $myJSON;
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Default button type is submit. Use onsubmit with event.preventDefault() inside onsubmit callback. If you don't need browser's form validation, you can use more straightforward type="button". For refreshing page look at location.reload().

about 4 years ago · Juan Pablo Isaza Denunciar

0

addPaxNameButton.addEventListener('click', (e) => {
    e.preventDefault();
    const name = addPaxNameField.value;
    updateGuestName(paxid,name);
  })
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda