HTML JS

SUBMIT HTML FORM WITHOUT RELOADING PAGE

(quick & simple examples)

<form id="myForm"            onsubmit="return doAJAX()">   Name: <input type="text" name="name">   Email: <input type="email" name="email">   <input type="submit" value="Go!">  </form>

HTML FORM

01

function doAJAX () {    GET FORM DATA   var data = new FormData   (document.getElementById("MyForm"));     AJAX   var xhr = new XMLHttpRequest();      xhr.open("POST", "SERVER.SCRIPT");

AJAX SUBMISSION

02

  xhr.onload = function () {      DO SOMETHING ON SERVER RESPONSE     console.log(this.response);   };    xhr.send(data);     PREVENT HTML FORM SUBMIT      return false; };

function doAJAX () {    GET FORM DATA   var data = new FormData   (document.getElementById("MyForm"));     AJAX   fetch("SERVER.SCRIPT", { method: "post",   body: data })

FETCH SUBMISSION

03

  .then(res => res.text())   .then(txt => {     DO SOMETHING ON SERVER RESPONSE     console.log(txt);   })   .catch(err => console.log(err));     PREVENT HTML FORM SUBMIT      return false; };