Javascript/Node/EJS 中用于发布的多个提交按钮

发布于 2025-01-18 03:27:47 字数 1909 浏览 1 评论 0原文

因此,我正在学习JavaScript,而我正在练习的一件事是各种模型视图控制器。目前,我有一个具有多个EJS模板的表格,但只有第一个与此问题有关。

第一个是家。

<DOCTYPE html>
    <html>
        <head>
            <title> HomeTitle </title>
            <style>

                body{background: skyblue;}

            </style>
            <body>
                <p> Welcome Home</p>
                <div>
                    
                 <form id ="redirect" method="POST">
                      <input type="submit"value="SignIn" >
                      <input type="submit" value="Exit" >
                    </form>
            </body>
            <script>
            </script>
            
        </head>
    </html>

我也有一个路由器。将作为路由器文件处理。

// Importing the module
const express=require("express")
var bodyParser=require('body-parser')
  
// Creating express Router
const router=express.Router()
var jsonParser= bodyParser.json()
var urlencodedParser =bodyParser.urlencoded({extended: false});
// Handling login request
router.get("/",(req,res,next)=>{
  res.render('home.ejs')
})


router.post("/", urlencodedParser, function (req,res){
  return res.redirect("/login");
})

router.get("/login",(req,res,next)=>{
  res.render("profile.ejs")
})


router.post("/login", urlencodedParser, function (req,res){

  console.log(req.body)
  //res.send(req.body.first)
  return res.redirect('/');

})
module.exports=router

在我的家中。ejs我有两个提交按钮,一个用于登录,一个用于退出。目前,按两个按钮将我带到页面中的符号(profile.ejs)我正在渲染(b/c只有一个渲染目的地)。有没有办法让我区分Javscript部分中的哪个按钮,以便我可以选择要呈现的页面。例如,按下符号按钮将渲染一个EJS模板,而退出将转到我将渲染的另一个模板。

第一个路由器。post是邮政功能,理想情况下,区分解决方案是因为我根据帖子数据处理新页面的地方。

So I'm learning Javascript and one of the things I'm practicing is a model view controller of sorts. Right now I have a form with multiple ejs templates, but only the first one pertains to this question.

The first is Home.ejs

<DOCTYPE html>
    <html>
        <head>
            <title> HomeTitle </title>
            <style>

                body{background: skyblue;}

            </style>
            <body>
                <p> Welcome Home</p>
                <div>
                    
                 <form id ="redirect" method="POST">
                      <input type="submit"value="SignIn" >
                      <input type="submit" value="Exit" >
                    </form>
            </body>
            <script>
            </script>
            
        </head>
    </html>

I also have a router.js that handles as the router file.

// Importing the module
const express=require("express")
var bodyParser=require('body-parser')
  
// Creating express Router
const router=express.Router()
var jsonParser= bodyParser.json()
var urlencodedParser =bodyParser.urlencoded({extended: false});
// Handling login request
router.get("/",(req,res,next)=>{
  res.render('home.ejs')
})


router.post("/", urlencodedParser, function (req,res){
  return res.redirect("/login");
})

router.get("/login",(req,res,next)=>{
  res.render("profile.ejs")
})


router.post("/login", urlencodedParser, function (req,res){

  console.log(req.body)
  //res.send(req.body.first)
  return res.redirect('/');

})
module.exports=router

In my Home.ejs I have two submit buttons, one for signing in, and one for exiting. At present, pressing either button takes me to the sign in page (profile.ejs) I am rendering (b/c there's only one render destination). Is there a way for me to differentiate which button was pressed in the javscript section so that I can chose which page I want to render. For example pressing the Sign In button would render one ejs template, whereas Exit would go to another template that I would render.

The first router.post is the post function where ideally the solution for differentiating would be since that is where I handle rendering the new page based on the post data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡寂寞咖啡 2025-01-25 03:27:47

您可以为输入指定名称:

<input type="submit" value="SignIn" name="submit">
<input type="submit" value="Exit" name="submit">

然后检查提交的输入的值并根据该值进行渲染/重定向:

const submit = req.body.submit;
if(submit === "SignIn"){
  //Do something
} else if(submit === "Exit"){
  //Do Something
} else {
}

You can assign a name to your inputs:

<input type="submit" value="SignIn" name="submit">
<input type="submit" value="Exit" name="submit">

Then check the value of submitted input and render/redirect based on that value:

const submit = req.body.submit;
if(submit === "SignIn"){
  //Do something
} else if(submit === "Exit"){
  //Do Something
} else {
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文