-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
188 lines (143 loc) · 4.31 KB
/
Copy pathindex.html
File metadata and controls
188 lines (143 loc) · 4.31 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<!DOCTYPE html>
<head>
<title>Sign Up</title>
<link href="index.css" rel="spreadsheet">
<style>
#picture img {
width:10vmax;
height:10vmax;
}
</style>
</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>
<label>Username: </label>
<input type='text' name='username'>
<br>
<div>
<label>Password</label>
<input type='password' name='password'>
</div>
<div>
<label>Verify Password</label>
<input type='password' name='verifyPassword'>
</div>
<div>
<label>Character</label>
<input type='file' name='picture' accept='image/*'>
<div id = 'picture'></div>
</div>
<br><br>
<button>SUBMIT</button>
</div>
<script>
var $preview = document.querySelector('#picture');
var imageData;
var $character = document.querySelector('input[type=file]');
$character.addEventListener('change', previewImage);
function previewImage() {
var fileReader = new FileReader();
fileReader.onload = function(e) {
var $img = document.createElement('img');
imageData = e.target.result;
$img.src = imageData;
$preview.innerHTML = '';
$preview.appendChild($img);
}
fileReader.readAsDataURL($character.files[0]);
}
function verifyPassword() {
var $passwords = document.querySelectorAll('input[type="password"]');
var isSamePassword = $passwords[0].value === $passwords[1].value;
if(!isSamePassword) {
alert("Please enter the same password!")
}
return isSamePassword;
}
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];
if ($input.type === 'text' || $input.type === 'password') {
data[$input.name] = $input.value;
} else if ($input.type === 'file') {
data[$input.name] = imageData;
}
//
// // 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) {
if (!verifyPassword()) {
return;
}
// 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');
// Alert the user to any errors from the server
function readyStateChange() {
if(req.readyState !== 4) return;
if(req.status !== 200) return;
data = JSON.parse(req.responseText);
if(data.error) {
return alert(data.error);
}
window.location.href = '/game';
}
req.onreadystatechange = readyStateChange;
// Sending the data in JSON format
req.send(JSON.stringify(data));
}
function submit() {
var formData = grabData();
if(!formData.picture) return alert('Please upload a picture');
sendData(formData);
}
function storeData() {
// Sending the data only if the passwords match
if(!verifyPassword()) return;
var data = {};
// In the function storeData:
for(var i = 0; i < $inputs.length; i++) {
var $input = $inputs[i];
if($input.type === 'text' || $input.type === 'password') {
data[$input.name] = $input.value;
} else if($input.type === 'checkbox' || $input.type === 'password') {
data[$input.name] = $input.checked;
} else if ($input.type === 'radio' && $input.checked || $input.type === 'password') {
data[$input.name] = $input.getAttribute('answer');
}
}
sendData(data);
}
var $button = document.querySelector('button')
$button.addEventListener('click', submit)
</script>
</body>
</html>