Web Analytics Made Easy -
StatCounter Post Form Data With Fetch Method and Check Responses - CodingForum

Announcement

Collapse
No announcement yet.

Post Form Data With Fetch Method and Check Responses

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Post Form Data With Fetch Method and Check Responses

    use the fetch method to submit a form how do you check reposnse and show hide lemnets accordingly please?
    I want to show hide two divs based on response, if OK show div1 & hide the form, if error show div2

    Code:
    document.addEventListener('DOMContentLoaded', function() {
      document.querySelector('.myForm').addEventListener('submit', function (event) {
      var data = this;
      fetch(data.getAttribute('action'), {
        method: data.getAttribute('method'),
        body: new FormData(data)
      }).then(res=>res.text())
      .then(function (data) {
    
      });
      event.preventDefault();
    
      });
    });
    Last edited by VIPStephan; May 10, 2022, 01:35 AM. Reason: corrected code BB tags
Working...
X