Use JSON post for answer setup

This commit is contained in:
Maxime Augier 2025-02-16 00:36:26 +01:00
parent 734cab1711
commit e360c3d487
2 changed files with 37 additions and 17 deletions

View File

@ -13,24 +13,26 @@
</style>
</head>
<body>
<form method="post" action="/choices">
<div id="question">
Question:
<input type="text" name="question">
</div>
<div id="rows">
<input type="text" name="choice" />
<input type="text" name="choice" />
<input type="text" name="choice" />
</div>
<input type="submit" value="OK">
<button type="button" onclick="more()">+</button>
<button type="button" onclick="less()">-</button>
</form>
<div>
Question:
<input id="question" type="text" name="question">
</div>
<div id="rows">
<input type="text" name="choice" />
<input type="text" name="choice" />
<input type="text" name="choice" />
</div>
<button type="button" onclick="poll()">Ok</button>
<button type="button" onclick="more()">+</button>
<button type="button" onclick="less()">-</button>
</body>
<script>
const rows = document.getElementById("rows");
const question = document.getElementById("question");
function more() {
const row = document.createElement('input');
row.setAttribute("type", "text");
@ -40,5 +42,21 @@
function less() {
rows.removeChild(rows.lastChild);
}
function poll() {
body = JSON.stringify({
question: question.value,
choices: [...rows.children].map((i) => i.value)
});
console.log(body);
fetch('/choices', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: body
}).then(() => {
window.location = '/#abstain'
});
}
</script>
</html>

View File

@ -49,7 +49,6 @@ app.use(session({
saveUninitialized: false,
secret: randomUUID(),
}))
app.use(formidable());
app.get('/', (req, res) => {
res.sendFile(page("index.html"));
@ -59,6 +58,7 @@ app.get('/login', (req,res) => {
res.sendFile(page("login.html"));
})
app.use('/login', formidable())
app.post('/login', (req, res) => {
if (req.fields.password !== admin_password) {
return res.sendStatus(403);
@ -67,14 +67,16 @@ app.post('/login', (req, res) => {
res.redirect('/choices');
})
app.use('/choices', admin_only);
app.use('/choices', express.json())
app.get('/choices', (req, res) => {
res.sendFile(page("choices.html"));
})
app.post('/choices', (req, res) => {
console.log(JSON.stringify(req.fields));
set_choices(req.fields.choice, req.fields.question);
console.log(JSON.stringify(req.body));
set_choices(req.body.choices, req.body.question);
res.redirect("/#abstain")
});