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.
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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…