gpt4 book ai didi

html - 如何使用 POST , GET 在 Nodejs 中制作搜索框

转载 作者:行者123 更新时间:2023-11-28 01:08:03 25 4
gpt4 key购买 nike

我想发布并从表单值中获取 getparameter 关键字并使用此查询

SELECT * from cider.cid_contents 
where con_content like \'%'+ keyword +'%\'
order by con_no desc;

但我不知道如何从 post 或 form 方法中获取关键字值。有代码

搜索.js

var express = require('express');
var router = express.Router();
var mysql = require("./model/mysql");

/* GET home page. */
router.post('/search/process', function(req, res, next) {

var keyword = req.body.keyword;

console.log(keyword);

res.redirect('/search');


});

router.get('/search/:keyword', function(req, res, next) {
var keyword;
keyword = req.params.keyword;
console.log("+++++");
console.log(keyword+"1234");
mysql.select('SELECT * from cider.cid_contents where con_content like \'%'+ keyword +'%\' order by con_no desc;',


function (err, data){
if (err) throw err;

res.render('front/search/search', { contents : data});
});
});


module.exports = router;

这是一个表单标签(top.ejs)

<form action="/search/process" method="post">
<input type="text" class="form-control web-search-box" placeholder="search" name="keyword" value=''></div>
<div id="search" class="search col-sm-1"><img src="../../page_imgs/fixed_img/icon_search.png"></div>
<input type="submit" value="send">
</form>

/搜索(search.ejs)

<table class="bordered">
<thead>
<tr>
<th data-field="no">num</th>
<th data-field="title">title</th>
<th data-field="date">date</th>
<th data-field="viewCount">count</th>
<th data-field=""></th>
</tr>
</thead>
<tbody>

<% for(var i = 0; i<contents.length; i++) { %>
<tr>

<td><%= contents[i].con_no %></td>
<td><a href="/adm/contents/detail/<%= contents[i].con_no %>"><%= contents[i].con_title %></a></td>
<td><%= contents[i].con_regDate %></td>
<td><%= contents[i].con_viewCount %></td>
<td><a href="/adm/contents/delete/<%= contents[i].con_no %>">delete</a></td>
</tr>
<% } %>
</tbody>
</table>

最佳答案

使用正文解析器。

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));

router.post('/search/process', function(req, res, next) {

console.log(req.body);

});

参见 https://scotch.io/tutorials/use-expressjs-to-get-url-and-post-parameters#post-parameters举个例子。

关于html - 如何使用 POST , GET 在 Nodejs 中制作搜索框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38946748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com