-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwith-ajax.html
106 lines (95 loc) · 3.97 KB
/
with-ajax.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
<title>MultiAction Forms</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="//time2hack.com">
<img src="https://time2hack.com/favicon.png" width="30" height="30" class="d-inline-block align-top" alt="" loading="lazy">
Time to Hack
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse d-flex" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item">
<a class="btn btn-outline-primary btn-sm m-1" href="index.html">With HTML5</a>
</li>
<li class="nav-item">
<a class="btn btn-outline-secondary btn-sm m-1 active" aria-current="page" href="with-ajax.html">With JS</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container p-3">
<h2 class="mb-5">Add/Update Address</h2>
<form id='addressForm' class="row g-3">
<div class="col-12">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" name="email" id="email">
</div>
<div class="col-12">
<label for="address1" class="form-label">Address</label>
<input type="text" class="form-control" name="address1" id="address1" placeholder="1234 Main St">
</div>
<div class="col-12">
<label for="address2" class="form-label">Address 2</label>
<input type="text" class="form-control" name="address2" id="address2" placeholder="Apartment, studio, or floor">
</div>
<div class="col-md-6">
<label for="city" class="form-label">City</label>
<input type="text" class="form-control" name="city" id="city">
</div>
<div class="col-md-4">
<label for="state" class="form-label">State</label>
<select name="state" id="state" class="form-select">
<option selected>Choose...</option>
<option>...</option>
</select>
</div>
<div class="col-md-2">
<label for="zip" class="form-label">Zip</label>
<input type="text" class="form-control" name="zip" id="zip">
</div>
<div class="col-12">
<button id='addAddress' type="submit" class="btn btn-primary">Add</button>
<button id='updateAddress' type="submit" class="btn btn-success">Update</button>
</div>
</form>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const form = document.querySelector('#addressForm')
const addButton = document.querySelector('#addAddress')
const updateButton = document.querySelector('#updateAddress')
addButton && addButton.addEventListener('click', (e) => {
e.preventDefault()
if (!form.checkValidity()) {
return
}
fetch('/.netlify/functions/addAddress', { method: 'POST', body: new FormData(form) })
.then((r => r.json()))
.then(console.log)
})
updateButton && updateButton.addEventListener('click', (e) => {
e.preventDefault()
if (!form.checkValidity()) {
return
}
fetch('/.netlify/functions/updateAddress', { method: 'POST', body: new FormData(form) })
.then((r => r.json()))
.then(console.log)
})
})
</script>
</body>
</html>