Skip to content

Instantly share code, notes, and snippets.

@Yaveen123
Created February 8, 2025 12:06
Show Gist options
  • Save Yaveen123/3f0d660ba6254eb547a6993549045602 to your computer and use it in GitHub Desktop.
Save Yaveen123/3f0d660ba6254eb547a6993549045602 to your computer and use it in GitHub Desktop.
Quick server side form processing with sqlite code
<form action="/api/editAdvancedSettings" method="POST" class="settings-container">
...
</form>
const editUserAccountDetails = `
UPDATE account
SET account_name = ?
WHERE google_id = ?
`; //SQL query
app.post('/api/editAccountSettings', (req, res) => { //"Alexander" (2016) How to get data passed from a form in Express (Node.js), accessed Jan 6 2025 https://stackoverflow.com/questions/9304888/how-to-get-data-passed-from-a-form-in-express-node-js
const { account_name, account_image, google_id } = req.body;
db.run(
editUserAccountDetails,
[account_name, google_id],
function(err) {
if (err) return console.error("Error at /api/editAccountSettings", err.message);
console.log(`SQL editUserAccountDetails, Rows updated ${this.changes}`);
});
res.statusCode = 302; // Redirects back.
res.setHeader("Location", "/html/settings-account.html"); // Nagle, D. (2016) How to res.send to a new URL in Node.js/Express?, Accessed Jan 6 2025 https://stackoverflow.com/questions/40497534/how-to-res-send-to-a-new-url-in-node-js-express#:~:text=You%20want%20to%20redirect%20the%20request%20by%20setting,permanent%20redirect.%20res.statusCode%20%3D%20302%3B%20res.setHeader%28%22Location%22%2C%20%22http%3A%2F%2Fwww.url.com%2Fpage%22%29%3B%20res.end%28%29%3B
res.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment