-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.html
122 lines (105 loc) · 4.03 KB
/
form.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Visitor Details Form</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
max-width: 600px;
}
label {
display: block;
margin-bottom: 8px;
}
input[type="text"], input[type="email"], input[type="file"] {
width: 100%;
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
#responseMessage {
margin-top: 15px;
font-size: 1rem;
}
</style>
</head>
<body>
<h2>Visitor Details Form</h2>
<form id="visitorForm">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="name">CIN:</label>
<input type="text" id="cin" name="cin" required>
<label for="address">purpose:</label>
<input type="text" id="purpose" name="purpose" required>
<label for="phone_number">Phone Number:</label>
<input type="text" id="phone_number" name="phone_number" required title="Enter a 10-digit phone number">
<label for="email">Email Address:</label>
<input type="email" id="email" name="email" required>
<h3>Upload Photos (up to 5):</h3>
<div id="image_path">
<label for="photo1">Photo 1:</label>
<input type="file" id="image_path" name="image_path" accept="image/*">
<!-- <label for="photo2">Photo 2:</label>
<input type="file" id="photo2" name="photos[]" accept="image/*">
<label for="photo3">Photo 3:</label>
<input type="file" id="photo3" name="photos[]" accept="image/*">
<label for="photo4">Photo 4:</label>
<input type="file" id="photo4" name="photos[]" accept="image/*">
<label for="photo5">Photo 5:</label>
<input type="file" id="photo5" name="photos[]" accept="image/*"> -->
</div>
<input type="submit" value="Submit">
</form>
<div id="responseMessage"></div>
<script>
document.getElementById('visitorForm').addEventListener('submit', async function(event) {
event.preventDefault();
const formData = new FormData(this);
const formObject = Object.fromEntries(formData.entries());
const token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEsImlhdCI6MTczNTUzNTA0OSwiZXhwIjoxNzM1NTM4NjQ5fQ.C-MGxDgzcn_R4kGgUs9TXJc4WmASVF3UwRb8KThuuHU"
try {
const response = await fetch('http://127.0.0.1:5000/visiter/add', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(formObject),
});
if (response.ok) {
const result = await response.json(); // Parse JSON response if backend sends it
document.getElementById('responseMessage').innerText = result.message || 'Form submitted successfully.';
document.getElementById('responseMessage').style.color = 'green';
} else {
const errorText = await response.text();
document.getElementById('responseMessage').innerText = `Error: ${errorText}`;
document.getElementById('responseMessage').style.color = 'red';
}
} catch (error) {
console.error('Error:', error);
document.getElementById('responseMessage').innerText = 'Error submitting form. Please try again.';
document.getElementById('responseMessage').style.color = 'red';
}
});
</script>
</body>
</html>