forked from digitnow/bacit-prog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform-test01.html
59 lines (54 loc) · 1.8 KB
/
form-test01.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!DOCTYPE html>
<html>
<head>
<!-- se https://developer.mozilla.org/en-US/docs/Glossary/character_encoding -->
<meta charset="UTF-8">
<style>
.body {
background: lightgreen;
}
.form-style {
#background: lightgrey;
font-size: 1.8em;
margin-bottom: 20px;
}
.input-style {
font-size: 1.2em;
}
.button-style {
font-size: 1.2em;
}
</style>
</head>
<body class="body">
<!-- Fra demonstrasjon i IS-114 "Intro i Programmering 4. oktober 2021 -->
<h3>Demonstrasjon av form-elementet</h3>
<!-- <form method="post" action="https://reqres.in/api/users"> -->
<form method="post">
<div class="form-style">
<label for="name">Navn:</label>
<input class="input-style" maxlength="10" type="text" name="name" id="name">
<button class="button-style" type="submit">Lagre</button>
</div>
</form>
<script>
//document.sessionStorage["form-data"] = $('this').serialize();
//document.location.href = 'another-page.html';
// I another-page.html
//
function onPost(event) {
event.preventDefault();
const data = new FormData(event.target);
console.log(data.entries());
const formJSON = Object.fromEntries(data.entries());
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
// https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
sessionStorage["from-post"] = JSON.stringify(formJSON, null, 2);
location.href = 'sink.html';
}
const form = document.querySelector('form');
form.addEventListener('submit', onPost);
// Sjekk også https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage (2021-10-04)
</script>
</body>
</html>