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
// This val serves an HTML page with an input field to enter a name. Upon form submission, it greets the user with the entered name in a pop style.
// We'll enhance the UI with a modern pop effect for the greeting message.
export default async function(req: Request): Promise<Response> {
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Greetings Form</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.content {
background-color: #3498db;
color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
input[type="text"] {
padding: 8px;
font-size: 16px;
margin-right: 10px;
}
input[type="submit"] {
padding: 8px 20px;
background-color: #4caf50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.pop {
animation: pop 0.5s forwards;
}
@keyframes pop {
0% { transform: scale(0); opacity: 0; }
100% { transform: scale(1); opacity: 1; }
}
</style>
</head>
<body>
<div class="content">
<h1>Greetings Form</h1>
<form id="greetingForm" method="post">
<label for="name">Enter your name:</label>
<input type="text" id="name" name="name" required>
<input type="submit" value="Submit">
</form>
</div>
<div id="greetingMessage" style="display:none;"></div>
<script>
document.getElementById("greetingForm").addEventListener("submit", async function(e) {
e.preventDefault();
const formData = new FormData(this);
const response = await fetch(location.href, { method: "POST", body: formData });
const data = await response.text();
document.getElementById("greetingMessage").innerHTML = data;
document.getElementById("greetingMessage").style.display = "block";
document.getElementById("greetingMessage").classList.add("pop");
});
</script>
</body>
</html>
`;
if (req.method === "POST") {
const formData = await req.formData();
const name = formData.get("name");
return new Response(`<h1>Hello, ${name}!</h1>`, { headers: { "Content-Type": "text/html" } });
}
return new Response(html, { headers: { "Content-Type": "text/html" } });
}