Use JSON post for answer setup
This commit is contained in:
parent
734cab1711
commit
e360c3d487
46
choices.html
46
choices.html
@ -13,24 +13,26 @@
|
|||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<form method="post" action="/choices">
|
|
||||||
<div id="question">
|
<div>
|
||||||
Question:
|
Question:
|
||||||
<input type="text" name="question">
|
<input id="question" type="text" name="question">
|
||||||
</div>
|
</div>
|
||||||
<div id="rows">
|
<div id="rows">
|
||||||
<input type="text" name="choice" />
|
<input type="text" name="choice" />
|
||||||
<input type="text" name="choice" />
|
<input type="text" name="choice" />
|
||||||
<input type="text" name="choice" />
|
<input type="text" name="choice" />
|
||||||
</div>
|
</div>
|
||||||
<input type="submit" value="OK">
|
<button type="button" onclick="poll()">Ok</button>
|
||||||
<button type="button" onclick="more()">+</button>
|
<button type="button" onclick="more()">+</button>
|
||||||
<button type="button" onclick="less()">-</button>
|
<button type="button" onclick="less()">-</button>
|
||||||
</form>
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
<script>
|
<script>
|
||||||
const rows = document.getElementById("rows");
|
const rows = document.getElementById("rows");
|
||||||
|
const question = document.getElementById("question");
|
||||||
|
|
||||||
function more() {
|
function more() {
|
||||||
const row = document.createElement('input');
|
const row = document.createElement('input');
|
||||||
row.setAttribute("type", "text");
|
row.setAttribute("type", "text");
|
||||||
@ -40,5 +42,21 @@
|
|||||||
function less() {
|
function less() {
|
||||||
rows.removeChild(rows.lastChild);
|
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>
|
</script>
|
||||||
</html>
|
</html>
|
8
index.js
8
index.js
@ -49,7 +49,6 @@ app.use(session({
|
|||||||
saveUninitialized: false,
|
saveUninitialized: false,
|
||||||
secret: randomUUID(),
|
secret: randomUUID(),
|
||||||
}))
|
}))
|
||||||
app.use(formidable());
|
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
res.sendFile(page("index.html"));
|
res.sendFile(page("index.html"));
|
||||||
@ -59,6 +58,7 @@ app.get('/login', (req,res) => {
|
|||||||
res.sendFile(page("login.html"));
|
res.sendFile(page("login.html"));
|
||||||
})
|
})
|
||||||
|
|
||||||
|
app.use('/login', formidable())
|
||||||
app.post('/login', (req, res) => {
|
app.post('/login', (req, res) => {
|
||||||
if (req.fields.password !== admin_password) {
|
if (req.fields.password !== admin_password) {
|
||||||
return res.sendStatus(403);
|
return res.sendStatus(403);
|
||||||
@ -67,14 +67,16 @@ app.post('/login', (req, res) => {
|
|||||||
res.redirect('/choices');
|
res.redirect('/choices');
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
app.use('/choices', admin_only);
|
app.use('/choices', admin_only);
|
||||||
|
app.use('/choices', express.json())
|
||||||
app.get('/choices', (req, res) => {
|
app.get('/choices', (req, res) => {
|
||||||
res.sendFile(page("choices.html"));
|
res.sendFile(page("choices.html"));
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post('/choices', (req, res) => {
|
app.post('/choices', (req, res) => {
|
||||||
console.log(JSON.stringify(req.fields));
|
console.log(JSON.stringify(req.body));
|
||||||
set_choices(req.fields.choice, req.fields.question);
|
set_choices(req.body.choices, req.body.question);
|
||||||
res.redirect("/#abstain")
|
res.redirect("/#abstain")
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user