-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_post.js
More file actions
48 lines (46 loc) · 1.75 KB
/
js_post.js
File metadata and controls
48 lines (46 loc) · 1.75 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
document.addEventListener("DOMContentLoaded", function () {
const insertForm = document.querySelector(".insert-form");
const idInput = document.querySelector("#st_id");
const nameInput = document.querySelector("#st_name");
const namesensorInput = document.querySelector("#sensor_name");
const typesensorInput = document.querySelector("#sensor_type");
const unitsensorInput = document.querySelector("#sensor_unit");
const valuesensorInput = document.querySelector("#sensor_value");
function insertSubmit(event) {
event.preventDefault(); //ป้องกันไม่ให้ form submit ตัวเอง
const data = {
st_id: idInput.value,
st_name: nameInput.value,
sensor_name: namesensorInput.value,
sensor_type: typesensorInput.value,
sensor_unit: unitsensorInput.value,
sensor_value: valuesensorInput.value,
};
console.log(sensor_value);
Swal.fire({
icon: 'success',
title: 'Successfully',
showConfirmButton: true
})
fetch("https://midterm-exam-010723313-2022.herokuapp.com/student_post", {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8' //บอกว่าข้อมูลที่จะส่งให้ API คือข้อมูลเเบบ JSON
},
body: JSON.stringify(data) //เเปลง OBJECT เป็น string
})
.then((res) => res.json())
.then((json) => {
})
.catch((error) => {
console.log(error.message);
});
idInput.value = '';
nameInput.value = '';
namesensorInput.value = '';
typesensorInput.value = '';
unitsensorInput.value = '';
valuesensorInput.value = '';
}
insertForm.addEventListener("submit", insertSubmit);
})