Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
616 views
in Technique[技术] by (71.8m points)

mysql - Get selected value from drop down and use it in router.get Nodejs

Basically what I want to do is to get a value from a drop-down list. The submit button will direct to another page and there I have a database query that uses the selected value.

enter image description here

parks.ejs

<div class="table-data">
        <h2>Display Data using Node.js & MySQL</h2>
            <table>
                <tr>
                    <th>ID</th>
                    <th>Station N</th>
                    
                    <th>Edit</th>
                    <th>Delete</th>
                </tr>
                
                <%
                if(userdataa.length!=0){
                var i=1;
                userdataa.forEach(function(data){
                %>
                <tr>
                    <td><%=i; %></td>
                    <td><%=data.Station_ID %></td>
                    
                    <td><a href="/users/edit/<%=data.id%>">Edit</a></td>
                    <td><a href="/users/delete/<%=data.id%>">Delete</a></td>
                </tr>
                <%  i++; }) %>
                <% } else{ %>
                    <tr>
                        <td colspan="7">No Data Found</td>
                    </tr>
                <% } %>
            </table>
            </div>

the form in user-list.ejs

<form action="/form-data" method="POST">
          <fieldset style="width:50%">
          <select class="selectpicker" data-style="btn-info" name="selectpicker">
            <optgroup label="Select Table">
                <option name="" value="0">Starting Station</option>
                <option name="table1" value="1">S</option>
                <option name="table2" value="2">B</option>
                <option name="table3" value="3">C</option>
            </optgroup>
        
       
    </select>
    <button href="/users/parks" class="dropbtn"><b>GO</b></button>

      </fieldset>
         </form>

the current users.js

var express = require('express');
var router = express.Router();
var db = require('../database');



router.get('/user-list', function(req, res, next) {

    db.query("SELECT Station_ID FROM Station WHERE Name = 'A'", function (err, results, fields) {
        if (err) throw err;
        res.render('user-list', { title: 'User List', userData: results});
  });
});


/*
router.post('/submit-forms',function(req,res,next){

    window.variabled = req.body.selectpicker;
})
*/


router.get('/parks', function(req, res, next) {

    db.query("SELECT Station_ID FROM Station WHERE Name = ?",[variabled], function (err, results, fields) {
        if (err) throw err;
        res.render('parks', { title: 'User Listt', userdataa: results});
  });
});

module.exports = router;

Now, I am stuck at how to store the value selected in the variable and use it in router.get. Also, please check my form code that the routes are correctly placed.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...