title: How to get value from .ejs to javascript file
body: <p>I have a .ejs file, which is actually a drop down form, that fills data using sqlite3 database. Now what i want to do is, when i select a value from dropdown, i want to send it back to my javascript file, where i'd save it to database.</p>
  
  <p>Normally this wasn't hard on a select statement which i made on my own, but as this select statement gets filled from javascript, the value that sends back is <strong>undefined</strong>, don't know why.</p>
  
  <p>To sum up on example:</p>
  
  <p>I have a user that is logged in, and has option to save a workout on a dropdown.</p>
  
  <p><strong>Workout table</strong></p>
  
  <p>ID: 5</p>
  
  <p>Name: Biceps</p>
  
  <p><strong>Result</strong></p>
  
  <p><strong>Exercise</strong></p>
  
  <p>ID:1</p>
  
  <p>Name: Biceps</p>
  
  <p>Workout-ID: 5</p>
  
  <p><strong>My code</strong></p>
  
  <p><strong>Javascript to .ejs</strong></p>
  
  <pre><code>var Workout = bookshelf.Model.extend({
  tableName: 'workout'
  });
  
  var Exercise = bookshelf.Model.extend({
  tableName: 'exercise',
  workout: function()
  {
      return this.hasMany(Workout)
  }
  });
  
  router.get('/', function (req, res) {
  new Vaje().fetchAll().then(function (workout) {
      res.render('Exercise', { workout: workout });
  }).catch(function (error) {
      console.log(error);
  });
  });
  </code></pre>
  
  <p>This sends all of the data from <strong>workout</strong> table into select form on .ejs</p>
  
  <p><strong>.ejs file</strong></p>
  
  <pre><code>&lt;h2&gt;Select workout&lt;/h2&gt;
  &lt;select&gt;
  &lt;% workout.forEach(function(w) { %&gt;
  &lt;option id=""
  &lt;%=w.attributes.id%&gt;"&gt;
  &lt;%= w.attributes.name %&gt;
  &lt;/option&gt;
  &lt;% }); %&gt;
  &lt;/select&gt;
  &lt;br&gt;&lt;/br&gt;
  &lt;form method="post"&gt;
  &lt;input type="submit" value="Add workout" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>javascript file</strong></p>
  
  <p>This file should now get selected value and save it to database...</p>
  
  <pre><code>router.post('/', function (req, res) {
  
  var new_workout = req.body;
  
  console.log("Workout: " + JSON.stringify(new_workout));
  
  new Exercise().save(new_workout);
  });
  </code></pre>
  
  <p><strong>Result from console</strong></p>
  
  <p><a href="https://i.stack.imgur.com/0R9cJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0R9cJ.jpg" alt="enter image description here"></a></p>
  
  <p>I have no idea why the value is undefined/empty, but i would sure as hell like to find out.</p>
  
  <p>Any help will be much appreciated!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p><a href="https://i.stack.imgur.com/gM3Nb.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gM3Nb.jpg" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/V3ulq.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/V3ulq.jpg" alt="enter image description here"></a></p>
  
  <p><strong>UPDATE2</strong></p>
  
  <p><a href="https://i.stack.imgur.com/6J9BH.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6J9BH.jpg" alt="enter image description here"></a></p>
  
  <p><strong>SOLUTION</strong></p>
  
  <pre><code>router.post('/', function (req, res) {
  new Vaje({ 'id': parseInt(req.body.naziv) })
      .fetch()
      .then(function (new_workout) {
          if (new_workout != null)
              new Trening().save({
                  vaje_id: new_workout.get("id"),
                  naziv: new_workout.get("naziv")
              });
      });
  });
  </code></pre>