点击表单中的按钮没有任何反应。

huangapple go评论108阅读模式
英文:

Clicking on button in form doesnt do anything

问题

这是一个使用NodeJS创建的试验性登录界面。代码用于创建一个登录界面,但是提交按钮没有任何反应。这段代码缺少什么?按钮没有将用户导向我创建的仪表板界面。

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><%=title%></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="/static/style.css">
</head>
<body>
    <h1>LMS</h1>
    <form>
        <div class="text-center center-div " id="login">
            <div class="container w-25 py-5">
                <div class="title pb-5">
                    <h2 class="font-weight-bold">Login System</h2>
                    <span> Login for the existing user</span>
                </div>
                <form action="/route/login" method="post" class="pt-3">
                    <div class="form-group">
                        <input type="email" class="form-control" id="exampleInputEmail1" name="email" placeholder="email">
                        <small class="form-text text-muted text-left">We'll never share your email with anyone else.</small>
                    </div>
                    <div class="form-group">
                        <input type="password" class="form-control" id="exampleInputPassword1" name="password" placeholder="password">
                    </div>

                    <button type="submit" class="btn btn-success rounded-pill">Submit</button>
                </form>
            </div>
        </div>
    </form>
</body>
</html>

ROUTER JS

var express = require("express");
var router = express.Router();

const credentials = { email: "rnwit@gmail.com", password: "Chennai@123" };

// 登录
router.post('/login', (req, res) => {
    if (req.body.email == credentials.email && req.body.password == credentials.password) {
        req.session.user = req.body.email;
        res.redirect("/route/dashboard");
    } else {
        res.end("Invalid username and password");
    }
});

// 仪表板
router.get('/dashboard', (req, res) => {
    if (req.session.user) {
        res.render('/dashboard', { user: req.session.user });
    } else {
        res.send('Unauthorised user');
    }
});

module.exports = router;

APP JS

const express = require("express");
const path = require("path");
const bodyparser = require("body-parser");
const session = require("express-session");
const { v4: uuidv4 } = require("uuid");

const router = require("./router");

const app = express();

const port = process.env.PORT || 8080;

app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true }));

app.set('view engine', 'ejs');

// 加载静态资源
app.use('/static', express.static(path.join(__dirname, 'public')));
app.use('/assets', express.static(path.join(__dirname, 'assets', 'images')));

app.use(session({
    secret: uuidv4(),
    resave: false,
    saveUninitialized: true
}));

app.use('/route', router);

// 路由
app.get('/', (req, res) => {
    res.render('base', { 'title': 'Login System' });
});

app.listen(port, () => { 'Listening on port 3000' });

请确保以下几点:

  1. 你的Node.js应用已经正确启动,并且没有报错。
  2. 你的路由配置正确,包括/login/dashboard的路径。
  3. 确保你的登录表单的提交按钮没有任何JavaScript代码或事件处理器来阻止其默认行为。

如果以上条件都满足,但问题仍然存在,可能需要检查浏览器的控制台是否有任何错误消息,以帮助进一步诊断问题。

英文:

This is a trial login screen using NodeJS.. The code is for a login screen but the submit button doesn't do anything. What does this code missing? The button doessnt direct to the dashboard screen i had created.

HTML

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt; &lt;%=title%&gt; &lt;/title&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD&quot; crossorigin=&quot;anonymous&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;/static/style.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;&lt;h1&gt; LMS&lt;/h1&gt;
&lt;form&gt;
&lt;div class=&quot;text-center center-div &quot;id=&quot;login&quot;&gt;
&lt;div class=&quot;container w-25 py-5&quot;&gt;
&lt;div class=&quot;title pb-5&quot;&gt;
&lt;h2 class=&quot;font-weight-bold&quot;&gt;Login System&lt;/h2&gt;
&lt;span&gt; Login for the existing user&lt;/span&gt;
&lt;/div&gt;
&lt;form action=&quot;/route/login&quot; method=&quot;post&quot; class=&quot;pt-3&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;input type=&quot;email&quot; class=&quot;form-control&quot; id=&quot;exampleInputEmail1&quot; name=&quot;email&quot; placeholder=&quot;email&quot;&gt;
&lt;small class=&quot;form-text text-muted text-left&quot;&gt;We&#39;ll never share your email with anyone else.&lt;/small&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;input type=&quot;password&quot; class=&quot;form-control&quot; id=&quot;exampleInputPassword1&quot; name=&quot;password&quot; placeholder=&quot;password&quot;&gt;
&lt;/div&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-success rounded-pill&quot;&gt;Submit&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;          
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

ROUTER JS

var express=require(&quot;express&quot;);
var router=express.Router();
const credentials={email:&quot;rnwit@gmail.com&quot;,password:&quot;Chennai@123&quot;}
//login
router.post(&#39;/login&#39;,(req,res)=&gt;{
if (req.body.email==credentials.email &amp;&amp; req.body.password==credentials.password){
req.session.user=req.body.email;
res.redirect(&quot;/route/dashboard&quot;);
//res.end(&quot;login successs&quot;);
}else{
res.end(&quot;Imvalid username and password&quot;);
}
});
//dashboard
router.get(&#39;/dashboard&#39;,(req,res)=&gt;{
if(req.session.user){
res.render(&#39;/dashboard&#39;,{user:req.session.user})
}else{
res.send(&#39;Unauthorised user&#39;)
}
})
module.exports=router;

APP JS

const express = require(&quot;express&quot;);
const path=require(&quot;path&quot;);
const bodyparser=require(&quot;body-parser&quot;);
const session=require(&quot;express-session&quot;);
const {v4:uuidv4}=require(&quot;uuid&quot;);
const router=require(&quot;./router&quot;);
const app = express();
const port=process.env.PORT ||8080;
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended:true}));
app.set(&#39;view engine&#39;,&#39;ejs&#39;);
//LOAD STATIC ASSETS
app.use(&#39;/static&#39;,express.static(path.join(__dirname,&#39;public&#39;)));
app.use(&#39;/assets&#39;,express.static(path.join(__dirname,&#39;assets&#39;,&#39;images&#39;)));
app.use(session({
secret:uuidv4(),
resave:false,
saveUninitialized:true
}));
app.use(&#39;/route&#39;,router);
//ROUTE
app.get(&#39;/&#39;,(req,res)=&gt;{
res.render(&#39;base&#39;,{&#39;title&#39;:&#39;Login System&#39;})
})
app.listen(port,()=&gt;{&#39;Listening on port 3000&#39;});

Could anyone check and say what is missing here and whay the form submit button would not work?

答案1

得分: 0

当我移除了外部表单时,它起作用了。谢谢。

英文:

As Teemu commented,(Nested forms are not allowed. Because of the invalid HTML, browsers strip the inner form and include all the form elements in the outer form, which in your case doesn't have action attribute. –
Teemu)
when i removed the outer form, it worked. Thanks

huangapple
  • 本文由 发表于 2023年1月9日 16:11:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定