【问题标题】:Disable button and enable it when id is found in database在数据库中找到 id 时禁用按钮并启用它
【发布时间】:2020-06-04 14:09:21
【问题描述】:

我正在使用 NodeJs 和 MySql 制作应用程序。我正在收集学生们关于他们最喜欢的演员的名字的条目。但我希望只有那些学生可以填写其 id 已经在我数据库的另一个表中的表格,没有新学生可以输入他/她对该表的兴趣。所以我想禁用提交按钮,首先想在nodejs中运行一个查询,其中学生ID等于现有表中的学生ID...

app.post("/register", function(req, res){

 var q= "INSERT INTO students_detail (student_id,actor_name) VALUES ('" + req.body.student_id + "', '" + req.body.actor_name + "' )"
connection.query(q, function(err, result) {
    if (err) res.send("You cant enter Two times");
    else res.redirect("/");
});

});

这是发布数据的代码,以我创建的 SQL 表的形式提供。

<head>
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400" rel="stylesheet">
<link rel="stylesheet" href="/app.css">

<div class="flex-container">
<div class="container">
    <h1><B>Student Suggesions</h1></B>
    <p class="lead"> Choose Your Actor </p>
    <form method="POST" action='/register'>
        <input type="number" class="form" name="student_id" placeholder="Enter Your ID Number"><br>
        <input type="text" class="form" name="actor_name" placeholder="Name of actor"> <br>
        <button>Vote Now</button>    

</div>

这是表单的 HTML。

【问题讨论】:

  • 向我们展示一些您尝试过的代码。
  • @Grumpy Sir,完成,请检查一次,我将不胜感激。

标签: html mysql sql node.js


【解决方案1】:
  1. 首先创建您的提交按钮并确保它有一个 id 并默认禁用它。将现有代码&lt;button&gt;Vote Now&lt;/button&gt;修改为&lt;input type="submit" value="Vote Now" id="button1" disabled&gt;

  2. 执行此查询:SELECT COUNT(student_id) AS STUDENT_COUNT FROM STUDENTS_CAN_FILL_FORM_TABLE WHERE student_id = &lt;student_id_to_search_for&gt;。如果 STUDENT_COUNT > 0 这意味着学生可以提交他或她的投票。使用此 javascript 启用上面的禁用按钮:document.getElementById("button1").disabled = false; 要再次禁用它,您可以使用 document.getElementById("button1").disabled = true;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-24
    • 2018-07-17
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多