I have a form where users enter data that includes a request for more information. This data is stored in a MongoDB database. Administrators can view each entry in a table on a separate page called list-applicants.html, with a checkbox in the last column of every row that can mark the request as 'complete' for each row/entry. The problem I run into is that checkbox is not being stored in the database.
Data is first stored in a POST routine in my server.js file.
app.post('/api/applicants', async (req, res) => {
console.log('You posted from a form!');
console.log(req.body);
const data = { ...req.body, dateOfApplication: new Date() };
const collection = client.db("applications").collection("applicants");
try {
await collection.insertOne(data);
} catch (error) {
console.log(error.message);
}
res.status(201);
res.redirect('/list-applicants.html');
});
In my list-applicants.html file I have the following:
async function getAllApplicants() {
const response = await fetch('/api/applicants');
const data = await response.json();
const { MongoClient, ServerApiVersion } = require('mongodb');
const dburi = process.env.DATABASE_URI;
const client = new MongoClient(dburi, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
const tableContent = document.getElementById('table-content');
for (const applicant of data) {
tableContent.innerHTML += `
<tr>
<td>${applicant.fname}</td>
<td>${applicant.lname}</td>
<td>${applicant.email}</td>
<td>${applicant.phone}</td>
<td>${applicant.campus}</td>
<td>${applicant.request}</td>
<td>${applicant.dateOfAttendance}</td>
<td><input type="checkbox" id="requestComplete" value="complete"></td>
</tr>
`
}
}
I understand I need to somehow do a POST to the MongoDB from the list-applicants.html file. I am just not quite sure how to do that in my list-applicants.js file.