动态下拉节点js

时间:2018-08-06 23:10:24

标签: javascript node.js express

我对节点和编程都非常陌生。我需要帮助弄清楚如何创建动态下拉列表。如果用户选择了产品类别,则在第二个下拉列表中选择相应的产品。下面是我的app.js文件

var express = require("express");
var app = express();
var mysql = require("mysql");
app.set("view engine","ejs");
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));

//create connection
var db = mysql.createConnection({
  host     : 'localhost',
  user     : 'adalugoda',
  password : "",
  database : "nodemysql",
  port      : 3306
}); 

app.get("/data",(req, res) => {
    var sql = 'SELECT DISTINCT category FROM gameinfo';
    var query = db.query(sql, (err,results) =>{
        if(err) throw err;

        res.render("data",{results:results});

    });
});

**//THIS PART WORKS IF IT GOES TO A DIFFERENT ROUTE**
app.post("/addgame", (req, res)=>{
    var newCategoryA = req.body.newCategoryD;
    //
    var sql = "SELECT DISTINCT gamename FROM gameinfo WHERE category ='"+ newCategoryA + "'";
    var query = db.query(sql,(err,results) =>{
      if(err) throw err;
      res.render("addgame",{results:results});
    });

});
app.get("/", function(req, res){
  res.render ("home");
});

MY ejs文件     

游戏类别页面

  <form action="/addgame" method ="POST">
      <select name ="newCategoryD">
          <% for (var i = 0; i < results.length; i++){ %>
              <option>
                  <%=results[i].category %>
              </option>
          <% } %>
      </select>
      <button>select this</button>

  </form>

非常感谢您的所有帮助

0 个答案:

没有答案