Save checked Chechbox into sqlite database with javascript
up vote
0
down vote
favorite
I have a form in my ejs with several checkboxes. In the application the user should be able to choose his interests by checking the checkboxes and this should be saved into a sqlite Database table.
I'm building the table via bootstrap:
db.exec('CREATE TABLE interests(interest text)');
(not sure which type to choose, so I set it to "text", but this is probably wrong?).
My Form looks like this
<form name="first_steps" action="first_steps" method="post">
<ul>
<li>
<input type="checkbox" name="interesse" value="theater" id="check1">Theater</label>
</li>
<li>
<input type="checkbox" name="interesse" value="musik" id="check2">Musik</label>
</li>
<li>
<input type="checkbox" name="interesse" value="party" id="check3">Party</label>
</li>
<li>
<input type="checkbox" name="interesse" value="sport" id="check4">Sport</label>
</li>
</ul>
<input type="submit" value="submit">
I found this Article Save result checkboxes checked in form into sqlite database with javascript and tried to convert this into my server.js
(Something like this
app.post('/first_steps', function(req, res) {
var elementen = document.getElementsByName ("interesse");
var tmpChoise;
for (var r= 0; r < elementen.length; r++) {
if (elementen[r].checked) {
tmpChoise = elementen[r].value;
alert(tmpChoise);
db.run(`INSERT INTO interessen(interesse) VALUES (?)`, [interesse], function(err) {
return res.redirect('/profil');
});
}
}
});
but this doesn't work. It says "ReferenceError: document is not defined").
Can someone tell me how the right code would look like? I'm a bloody beginner and couldn't really figure anything out.
(Javascript, express, sqlite3, node.js)
javascript node.js express checkbox sqlite3
add a comment |
up vote
0
down vote
favorite
I have a form in my ejs with several checkboxes. In the application the user should be able to choose his interests by checking the checkboxes and this should be saved into a sqlite Database table.
I'm building the table via bootstrap:
db.exec('CREATE TABLE interests(interest text)');
(not sure which type to choose, so I set it to "text", but this is probably wrong?).
My Form looks like this
<form name="first_steps" action="first_steps" method="post">
<ul>
<li>
<input type="checkbox" name="interesse" value="theater" id="check1">Theater</label>
</li>
<li>
<input type="checkbox" name="interesse" value="musik" id="check2">Musik</label>
</li>
<li>
<input type="checkbox" name="interesse" value="party" id="check3">Party</label>
</li>
<li>
<input type="checkbox" name="interesse" value="sport" id="check4">Sport</label>
</li>
</ul>
<input type="submit" value="submit">
I found this Article Save result checkboxes checked in form into sqlite database with javascript and tried to convert this into my server.js
(Something like this
app.post('/first_steps', function(req, res) {
var elementen = document.getElementsByName ("interesse");
var tmpChoise;
for (var r= 0; r < elementen.length; r++) {
if (elementen[r].checked) {
tmpChoise = elementen[r].value;
alert(tmpChoise);
db.run(`INSERT INTO interessen(interesse) VALUES (?)`, [interesse], function(err) {
return res.redirect('/profil');
});
}
}
});
but this doesn't work. It says "ReferenceError: document is not defined").
Can someone tell me how the right code would look like? I'm a bloody beginner and couldn't really figure anything out.
(Javascript, express, sqlite3, node.js)
javascript node.js express checkbox sqlite3
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a form in my ejs with several checkboxes. In the application the user should be able to choose his interests by checking the checkboxes and this should be saved into a sqlite Database table.
I'm building the table via bootstrap:
db.exec('CREATE TABLE interests(interest text)');
(not sure which type to choose, so I set it to "text", but this is probably wrong?).
My Form looks like this
<form name="first_steps" action="first_steps" method="post">
<ul>
<li>
<input type="checkbox" name="interesse" value="theater" id="check1">Theater</label>
</li>
<li>
<input type="checkbox" name="interesse" value="musik" id="check2">Musik</label>
</li>
<li>
<input type="checkbox" name="interesse" value="party" id="check3">Party</label>
</li>
<li>
<input type="checkbox" name="interesse" value="sport" id="check4">Sport</label>
</li>
</ul>
<input type="submit" value="submit">
I found this Article Save result checkboxes checked in form into sqlite database with javascript and tried to convert this into my server.js
(Something like this
app.post('/first_steps', function(req, res) {
var elementen = document.getElementsByName ("interesse");
var tmpChoise;
for (var r= 0; r < elementen.length; r++) {
if (elementen[r].checked) {
tmpChoise = elementen[r].value;
alert(tmpChoise);
db.run(`INSERT INTO interessen(interesse) VALUES (?)`, [interesse], function(err) {
return res.redirect('/profil');
});
}
}
});
but this doesn't work. It says "ReferenceError: document is not defined").
Can someone tell me how the right code would look like? I'm a bloody beginner and couldn't really figure anything out.
(Javascript, express, sqlite3, node.js)
javascript node.js express checkbox sqlite3
I have a form in my ejs with several checkboxes. In the application the user should be able to choose his interests by checking the checkboxes and this should be saved into a sqlite Database table.
I'm building the table via bootstrap:
db.exec('CREATE TABLE interests(interest text)');
(not sure which type to choose, so I set it to "text", but this is probably wrong?).
My Form looks like this
<form name="first_steps" action="first_steps" method="post">
<ul>
<li>
<input type="checkbox" name="interesse" value="theater" id="check1">Theater</label>
</li>
<li>
<input type="checkbox" name="interesse" value="musik" id="check2">Musik</label>
</li>
<li>
<input type="checkbox" name="interesse" value="party" id="check3">Party</label>
</li>
<li>
<input type="checkbox" name="interesse" value="sport" id="check4">Sport</label>
</li>
</ul>
<input type="submit" value="submit">
I found this Article Save result checkboxes checked in form into sqlite database with javascript and tried to convert this into my server.js
(Something like this
app.post('/first_steps', function(req, res) {
var elementen = document.getElementsByName ("interesse");
var tmpChoise;
for (var r= 0; r < elementen.length; r++) {
if (elementen[r].checked) {
tmpChoise = elementen[r].value;
alert(tmpChoise);
db.run(`INSERT INTO interessen(interesse) VALUES (?)`, [interesse], function(err) {
return res.redirect('/profil');
});
}
}
});
but this doesn't work. It says "ReferenceError: document is not defined").
Can someone tell me how the right code would look like? I'm a bloody beginner and couldn't really figure anything out.
(Javascript, express, sqlite3, node.js)
javascript node.js express checkbox sqlite3
javascript node.js express checkbox sqlite3
asked Nov 21 at 19:48
Joseffa Steuernagel
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Your server.js file should usually work data that has been posted from your form. Process should look something like:
- Display front end form
- Post front end form data to your server
- Manipulate or work with data on your server
- Send some response from your server back to the front end.
In your case, instead of trying to look at DOM elements that would result from document.getElementsByName ("interesse")
, you should probably be working with data stored on res.body
. So that might look something like:
app.post('/first_steps', function(req, res) {
data = req.body.interesse;
db.run(`INSERT INTO interessen(interesse) VALUES (?)`, [data], function(err) {
return res.redirect('/profil');
});
}
}
});
Or something similar. Check out this article for an example of getting post data, and then look at the data being posted from your form in a console log like
var postBody = req.body;
console.log(postBody);
(That console log output will be in whatever terminal your express server is running in)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Your server.js file should usually work data that has been posted from your form. Process should look something like:
- Display front end form
- Post front end form data to your server
- Manipulate or work with data on your server
- Send some response from your server back to the front end.
In your case, instead of trying to look at DOM elements that would result from document.getElementsByName ("interesse")
, you should probably be working with data stored on res.body
. So that might look something like:
app.post('/first_steps', function(req, res) {
data = req.body.interesse;
db.run(`INSERT INTO interessen(interesse) VALUES (?)`, [data], function(err) {
return res.redirect('/profil');
});
}
}
});
Or something similar. Check out this article for an example of getting post data, and then look at the data being posted from your form in a console log like
var postBody = req.body;
console.log(postBody);
(That console log output will be in whatever terminal your express server is running in)
add a comment |
up vote
0
down vote
Your server.js file should usually work data that has been posted from your form. Process should look something like:
- Display front end form
- Post front end form data to your server
- Manipulate or work with data on your server
- Send some response from your server back to the front end.
In your case, instead of trying to look at DOM elements that would result from document.getElementsByName ("interesse")
, you should probably be working with data stored on res.body
. So that might look something like:
app.post('/first_steps', function(req, res) {
data = req.body.interesse;
db.run(`INSERT INTO interessen(interesse) VALUES (?)`, [data], function(err) {
return res.redirect('/profil');
});
}
}
});
Or something similar. Check out this article for an example of getting post data, and then look at the data being posted from your form in a console log like
var postBody = req.body;
console.log(postBody);
(That console log output will be in whatever terminal your express server is running in)
add a comment |
up vote
0
down vote
up vote
0
down vote
Your server.js file should usually work data that has been posted from your form. Process should look something like:
- Display front end form
- Post front end form data to your server
- Manipulate or work with data on your server
- Send some response from your server back to the front end.
In your case, instead of trying to look at DOM elements that would result from document.getElementsByName ("interesse")
, you should probably be working with data stored on res.body
. So that might look something like:
app.post('/first_steps', function(req, res) {
data = req.body.interesse;
db.run(`INSERT INTO interessen(interesse) VALUES (?)`, [data], function(err) {
return res.redirect('/profil');
});
}
}
});
Or something similar. Check out this article for an example of getting post data, and then look at the data being posted from your form in a console log like
var postBody = req.body;
console.log(postBody);
(That console log output will be in whatever terminal your express server is running in)
Your server.js file should usually work data that has been posted from your form. Process should look something like:
- Display front end form
- Post front end form data to your server
- Manipulate or work with data on your server
- Send some response from your server back to the front end.
In your case, instead of trying to look at DOM elements that would result from document.getElementsByName ("interesse")
, you should probably be working with data stored on res.body
. So that might look something like:
app.post('/first_steps', function(req, res) {
data = req.body.interesse;
db.run(`INSERT INTO interessen(interesse) VALUES (?)`, [data], function(err) {
return res.redirect('/profil');
});
}
}
});
Or something similar. Check out this article for an example of getting post data, and then look at the data being posted from your form in a console log like
var postBody = req.body;
console.log(postBody);
(That console log output will be in whatever terminal your express server is running in)
answered Nov 21 at 20:18
Bbleds
312
312
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419531%2fsave-checked-chechbox-into-sqlite-database-with-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown