简体   繁体   English

在 js 执行某事后在 html 中显示字符串

[英]displaying string in html after js executes something

So, im trying to create a accounts system, i made a login system that works, but when you enter wrong credentials i want it to display "wrong username or password" in the value of ab empty所以,我试图创建一个帐户系统,我创建了一个可以正常工作的登录系统,但是当您输入错误的凭据时,我希望它在 ab 空值中显示“错误的用户名或密码”

tag, the thing is that i dont know how to access the标签,问题是我不知道如何访问

tag from app.js (node.js project)来自 app.js 的标签(node.js 项目)

<div class="form">          
      <form class="login-form" action="/account" method="post">
        <p id = "check"></p>
        <h1 id = "login-text">Login</h1>
        <label style="color:white;">Username:</label>   
        <input type="text" id = "username" placeholder="Enter Username" name="username" required>
        <label style="color:white;">Password:</label>  
        <input type="password" id = "password" placeholder="Enter Password" name="password" required>
        <button type="submit">Login</button>
        <a href="register"><h5>register</h5></a>
      </form>
    </div>
  </div>
app.post('/account', (req, res) => { 
  const username = req.body.username;
  const password = req.body.password;
  con.query("USE discbin")
  con.query('SELECT * FROM accounts WHERE username = ?', [username], function (err, result, fields) {
    if (err) throw err;
      if (result.length > 0 && result[0].password === password) {
        console.log(result[0].username + " logged in!")
        res.render('account');
      }
      else {
        console.log("a user tried to login using the username: " + username)
        //set <p> to "wrong username or password"
      }
    });
  });
document.getElementById("check").textContent = "wrong username or password";
  1. Get the element you want to update document.getElementById("check")获取要更新的元素document.getElementById("check")
  2. Set its textContent property to the text you want to use将其textContent属性设置为您要使用的文本

I think this is an XY problem .我认为这是一个XY 问题 As far as I know, it is not possible to access a DOM element directly from the server, however, you can achieve the same result with a little JavaScript on the client side.据我所知,无法直接从服务器访问 DOM 元素,但是,您可以在客户端使用一点 JavaScript 获得相同的结果。

For example the server response could be on authentication success a JSON object like that:例如,服务器响应可能是身份验证成功 a JSON object,如下所示:

{
  auth: true,
  //some token I guess...
}

and on authentication fail:并在身份验证失败时:

{ auth: false }

Then, you can use JavaScript to access the element you want and modify its content using the innerHTML method.然后,您可以使用 JavaScript 访问您想要的元素并使用 innerHTML 方法修改其内容。 In your case, you could store the string to be displayed and modify it according to the server response.在您的情况下,您可以存储要显示的字符串并根据服务器响应对其进行修改。

let checkStringVariable = "";

On server response:在服务器响应上:

checkStringVariable = response.auth ? "" : "wrong username or password";
document.getElementbById("check").innerHTML = checkStringVariable;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM