-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.html
81 lines (57 loc) · 1.76 KB
/
app.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
<!DOCTYPE html>
<head>
<title>Hi Yahhh</title>
<link href="index.css" rel="spreadsheet">
</head>
<body>
<div>
<label>Name: </label>
<input type='text' name='name'>
<br>
<label>Phone Number: </label>
<input type='text' name='phone'>
<br>
<label>Email: </label>
<input type='text' name='email'>
<br><br>
<button>SUBMIT</button>
</div>
<script>
function grabData() {
// Getting all the input tags
var $inputs = document.querySelectorAll('input');
// Defining a variable to store all the data
var data = {};
// Looping through all the input tags
for(var i = 0; i < $inputs.length; i++) {
// Getting the ith input tag
var $input = $inputs[i];
// Getting the name of the data that the input tag holds
var name = $input.name;
// Getting the value that the user entered into the input tag
var value = $input.value;
// Storing the value and its name
data[name] = value;
}
// Outputting the data
return data;
}
function sendData(data) {
// Creating a new request to the server that contains the data
var req = new XMLHttpRequest();
// Opening up a connection with the server to 'POST' data
req.open('POST', '/form', true);
// Explain to the server that we are sending the data in json format
req.setRequestHeader('content-type', 'application/json')
// Sending the data in JSON format
req.send(JSON.stringify(data));
}
function submit() {
var formData = grabData();
sendData(formData);
}
var $button = document.querySelector('button')
$button.addEventListener('click', submit)
</script>
</body>
</html>