HTML JAVASCRIPT

HOW TO GET HTML FORM DATA IN JAVASCRIPT

1) SINGLE FIELD

 <form onsubmit="return doForm()">    <input type="text" id="u_name"/>     <input type="submit" value="Go!"/> </form>

<script> function doForm () {   var data = new FormData();   data.append("name", document.   getElementById("u_name").value); } </script>

2) MULTIPLE FIELDS

<form id="u_form"             onsubmit="return doForm()">   <input type="text" name="u_name"/>   <input type="email" name="u_email"/>   <input type="submit" value="Go!"/> </form>

<script> function doForm () {   // NEW FORM DATA OBJECT   var data = new FormData();     // LOOP THROUGH ALL FIELDS   var all = document.     querySelectorAll("#u_form input");   for (let field of all) {     if (field.type != "submit") {      data.append(field.name,field.value);     }   } } </script>