Save checked Chechbox into sqlite database with javascript












0















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)










share|improve this question



























    0















    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)










    share|improve this question

























      0












      0








      0








      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)










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 19:48









      Joseffa SteuernagelJoseffa Steuernagel

      32




      32
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Your server.js file should usually work data that has been posted from your form. Process should look something like:




          1. Display front end form

          2. Post front end form data to your server

          3. Manipulate or work with data on your server

          4. 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)






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            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

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Your server.js file should usually work data that has been posted from your form. Process should look something like:




            1. Display front end form

            2. Post front end form data to your server

            3. Manipulate or work with data on your server

            4. 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)






            share|improve this answer




























              0














              Your server.js file should usually work data that has been posted from your form. Process should look something like:




              1. Display front end form

              2. Post front end form data to your server

              3. Manipulate or work with data on your server

              4. 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)






              share|improve this answer


























                0












                0








                0







                Your server.js file should usually work data that has been posted from your form. Process should look something like:




                1. Display front end form

                2. Post front end form data to your server

                3. Manipulate or work with data on your server

                4. 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)






                share|improve this answer













                Your server.js file should usually work data that has been posted from your form. Process should look something like:




                1. Display front end form

                2. Post front end form data to your server

                3. Manipulate or work with data on your server

                4. 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)







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 20:18









                BbledsBbleds

                312




                312






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Create new schema in PostgreSQL using DBeaver

                    Deepest pit of an array with Javascript: test on Codility

                    Costa Masnaga