Sobes.tech
Middle

Есть ли способ обработать форму не через стандартный submit? Как отправить форму через JavaScript?

sobes.tech AI

Answer from AI

Yes, it is possible to process a form without the standard submit, by using JavaScript to intercept the submit event and send data via AJAX or Fetch API.

Example of submitting a form using JavaScript with Fetch:

const form = document.querySelector('form');

form.addEventListener('submit', function(event) {
  event.preventDefault(); // Cancel the default behavior

  const formData = new FormData(form);

  fetch(form.action, {
    method: form.method,
    body: formData
  })
  .then(response => response.json())
  .then(data => {
    console.log('Success:', data);
  })
  .catch(error => {
    console.error('Error:', error);
  });
});

Thus, the form will not be submitted in the standard way, and data will be sent asynchronously via JavaScript.

Есть ли способ обработать форму не через стандартный… - sobes.tech