HTML文件
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>CHAT APPLICATION</title>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="../Project/CSS/sty.css">
</head>
<body>
<div class='container-fluid'>
<div class=row>
<div class='col-sm-12'>
<h1>Welcome to chat application</h1>
</div>
</div>
<div class=row>
<div class=' col-sm-12 text-center'>
<button type="button" class="btn btn-outline-primary button-press-1" data-target='#register' data-toggle='modal'>Register</button>
<button type="button" class="btn btn-outline-primary button-press-2" data-target='#login' data-toggle='modal'>Login</button>
</div>
</div>
</div>
<!-- connecting modal to the button for registration -->
<!-- Button trigger modal -->
<!-- Modal -->
<div class="modal fade" id="register">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Register</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="/" method="post">
<div class="form-group">
<label for="Username">Username</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter your username" name="uname">
<div class="form-group">
<label for="Userid">Userid</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="enter your userid" name="uid">
</div>
<div class="form-group">
<label for="Password">Password</label>
<input type="password" class="form-control" id="exampleCheck1" name="passwd">
</div>
<button type="submit" class="btn btn-primary btn-lg btn-block register-1">Register</button>
</form>
</div>
</div>
</div>
</div>
</div>
<!--connecting modal to button for login -->
<!-- Button trigger modal -->
<!-- Modal -->
<div class="modal fade" id="login">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Register</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="/" ,method="post">
<div class="form-group">
<label for="Userid">Userid</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="enter your userid">
</div>
<div class="form-group">
<label for="Password">Password</label>
<input type="password" class="form-control" id="exampleCheck1">
</div>
<button type="submit" class="btn btn-primary btn-block btn-lg login">Login</button>
</form>
</div>
</div>
</div>
</div>
</section>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="server.js" charset="utf-8"></script>
</body>
</html>
表达JS文件
var express = require("express");
var mysql = require('mysql');
var app = express()
var bodyParser = require("body-parser")
app.use(bodyParser.urlencoded({
extended: false}));
app.get("/", function(req, res) {
res.sendFile(__dirname + "/pro.html")
})
var conn = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'mydb'
});
conn.connect(function(err) {
if (err) {
console.log(err);
} else {
console.log("connected!!");
}
/*
conn.query("CREATE TABLE mytable (Username VARCHAR(255), Userid VARCHAR(255), Password VARCHAR(255))", function(err, result) {
if (err) {
console.log(err);
} else {
console.log("Tables created!!");
}
*/
app.post("/", function(req, res) {
const username = req.body.uname;
const userid = req.body.uid;
const pswd = req.body.passwd;
console.log(req.body);
var sql = "INSERT INTO mytable (Username,Userid,Password) VALUES (username,userid,pswd)";
conn.query(sql, function(err, result) {
if (err) {
console.log(err);
} else {
console.log("Records inserted successfully!!");
}
});
});
})
app.listen(3000)
我尝试了所有方法,例如将method='post'
设置为HTML形式。我有一个名为mydb的数据库,我想在其中插入数据,所以我正在使用post request从HTML表单获取数据,但是它不起作用。我想控制台记录我键入的表单的值,但是无法在终端中获取它。
答案 0 :(得分:0)
我希望这对您有帮助:-
var sql = "INSERT INTO mytable set Username='"+username+"',Userid='"+userid+"',Password='"+pswd+"'";
conn.query(sql, function(err, row) {
if (err) {
console.log(err);
} else {
console.log("Records inserted successfully!!");
}
});