Hi all, I have just started coding and am new to this community. I hope someone can help me out.
I am trying to save user input entered in a HTML form in a sqlite database file on my local disk using javascript. Is this possible and, if so, how must this be done?
My html file links to a javascript file that contains the following code (shortened for ease of reading):
const sqlite3 = require('sqlite3');
const db = new sqlite3.Database('./unverified_user_input.db');
db.run("CREATE TABLE unverified_user_input(id INTEGER, forename TEXT, surname TEXT)");
function storeUserInput(ev) {
ev.preventDefault();
let user_input = {
id: Date.now(),
forename: document.getElementById("Forename").value,
surname: document.getElementById("Surname").value,
}
db.run("INSERT INTO unverified_user_input
VALUES
($id, $forename, $surname)",
{
$id: user_input.id,
$forename: user_input.forename,
$surname: user_input.surname
});
document.addEventListener("DOMContentLoaded", ()=> {
document.getElementById("submission").addEventListener("click", storeUserInput);
});
When I try this I get the following error message:
Uncaught ReferenceError: require is not defined
Then, I tried to convert my JS file with browserify
but then I get this error:
Cannot read property ‘_handle’ of undefined
I found the exact same question on stackoverflow: https://stackoverflow.com/questions/59554423/how-to-connect-to-a-sqlite-db-from-an-html-file-via-javascript
However, still being a beginner, I do not understand the answers that were given on stackoverflow and I am still not sure whether what I am trying to achieve is at all possible.
I would be extremely grateful if someone could help me out.
Thanks!
Nick