Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 11, 2023 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/d788ea71ab12c51b777d66294e966af6 to your computer and use it in GitHub Desktop.
Save code-boxx/d788ea71ab12c51b777d66294e966af6 to your computer and use it in GitHub Desktop.
Javascript Get HTML Form Data

JAVASCRIPT GET HTML FORM DATA

https://code-boxx.com/get-html-form-data-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Form Data POST</title>
<meta charset="utf-8">
<link rel="stylesheet" href="x-cosmetics.css">
</head>
<body>
<!-- (A) HTML FORM -->
<form id="demoA" onsubmit="return doForm()">
<input type="text" name="name" required value="Jon Doe">
<input type="email" name="email" required value="jon@doe.com">
<input type="submit" value="Go!">
</form>
<!-- (B) JS PROCESSING -->
<script>
function doForm () {
// (B1) FORM DATA - AUTO GET ALL FIELDS FROM "#DEMOA"
var data = new FormData(document.getElementById("demoA"));
// (B2) AJAX FETCH
fetch("SERVER-SCRIPT", { method:"post", body:data })
.then(res => res.text())
.then(txt => console.log(txt))
.catch(err => console.error(err));
// (B3) PREVENT DEFAULT FORM SUBMIT
return false;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Form Data URL PARAMS</title>
<meta charset="utf-8">
<link rel="stylesheet" href="x-cosmetics.css">
</head>
<body>
<!-- (A) HTML FORM -->
<form id="demoB" onsubmit="return doForm()">
<input type="text" name="name" required value="Joe Doe">
<input type="email" name="email" required value="joe@doe.com">
<input type="submit" value="Go!">
</form>
<!-- (B) JS PROCESSING -->
<script>
function doForm () {
// (B1) FORM DATA - AUTO GET ALL FIELDS FROM "#DEMOB"
var data = new FormData(document.getElementById("demoB"));
// (B2) TO URL PARAMS
var params = new URLSearchParams(data).toString();
// (B3) AJAX FETCH
fetch("SERVER-SCRIPT?" + params)
.then(res => res.text())
.then(txt => console.log(txt))
.catch(err => console.error(err));
// (B4) OR REDIRECT - MIGHT AS WELL JUST SUBMIT THE FORM?
// location.href = "SERVER-SCRIPT?" + params;
// (B5) PREVENT DEFAULT FORM SUBMIT
return false;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Form Data APPEND</title>
<meta charset="utf-8">
<link rel="stylesheet" href="x-cosmetics.css">
</head>
<body>
<!-- (A) HTML FORM -->
<form id="demoC" onsubmit="return doForm()">
<input type="text" name="name" required value="Joy Doe">
<input type="email" name="email" required value="joy@doe.com">
<div id="birthday">12 Mar 2077</div>
<input type="submit" value="Go!">
</form>
<!-- (B) JS PROCESSING -->
<script>
function doForm () {
// (B1) FORM DATA - AUTO GET ALL FIELDS FROM "#DEMOC"
var data = new FormData(document.getElementById("demoC"));
// (B2) MANUALLY APPEND MORE
data.append("birthday", document.getElementById("birthday").innerText);
data.append("gender", "female");
data.append("key", "value");
// (B3) FORM DATA YOGA
data.delete("key");
var name = data.get("name"); // Joy Doe
var hasEmail = data.has("email"); // true
for (let k of data.keys()) { console.log(k); }
for (let v of data.values()) { console.log(v); }
for (let [k, v] of data.entries()) { console.log(k, v); }
// (B4) PREVENT DEFAULT FORM SUBMIT
return false;
}
</script>
</body>
</html>
/* (X) NOT IMPORTANT */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
form {
max-width: 600px;
padding: 20px;
border: 2px solid #ddd;
background: #f2f2f2;
}
input {
display: block;
width: 100%;
padding: 10px;
}
input[type=text], input[type=email] { margin-bottom: 10px; }
input[type=submit] { margin-top: 20px; cursor: pointer; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment