【问题标题】:How can I use form input values in my submission script?如何在提交脚本中使用表单输入值?
【发布时间】:2020-12-07 14:59:05
【问题描述】:

我正在尝试做一件简单的事情。我有一个网站,我希望人们能够发送一些反馈并警告我,因为我有一个“通知所有者”按钮,它显示一个表单,并且在提交时应该执行一个 python 脚本,向自己发送一封包含他们信息的电子邮件填满。

这是我的表单代码(index.html - 在客户端创建)

<script>
async function notifyOwner() {
  await fetch("api/notify", { method: "post" }).catch(console.log);
}
</script>

<div class="form-popup" id="myForm">
      <form class="form-container" name="form-owner">
        <h1>Notify Owner</h1>
        <label><b>Name</b></label>
        <input id="name" style="width:90%" type="text" placeholder="Enter your name$        
        <label><b>Message</b></label>
        <input id="context" style="width:90%" type="text" placeholder="Enter Reason$
        <button type="submit" class="btn" onclick="notifyOwner()">Submit</button>
        <button type="button" class="btn cancel" onclick="closeForm()">Close</butto$
      </form>
</div>

这是我的服务器端处理

app.post("/api/notify", async (req, res) => {
  try{
    var subject = "teste";
    var body = "ok";
    child_process.execSync('python3 sendEmail.py ' + subject + " " + body);
  }catch(error){
    console.error(error);
  }
});

我应该怎么做才能用 id="name" 中的值替换 var 主题并用 id="context" 中的值替换 var 主体?

【问题讨论】:

  • 我可以建议使用 nodejs 客户端发送电子邮件吗?从 nodeJS 应用程序调用 python 脚本,只是为了发送一封电子邮件。有很多可用的 nodejs 电子邮件库。
  • 您是在问如何在 JavaScript 中获取输入值吗?这回答了你的问题了吗? stackoverflow.com/questions/11563638/…
  • 我一定会试一试的!即使我已经搜索过了,也许我对 nodejs 的了解还不够,无法正确搜索它:D
  • @isherwood 我认为 document.getElementById 不起作用,因为服务器没有部署表单,表单是在客户端完成的,我在服务器端调用脚本。我对如何将数据从 html 传输到服务器更感兴趣
  • NodeJS 只是 JavaScript。拓宽研究范围。

标签: javascript node.js forms


【解决方案1】:

已解决

在服务器端。

app.use(express.json());
app.post("/api/notify", async (req, res) => {
    try{
        const subject = req.body.sub;
        const body = req.body.cont;
        child_process.execSync('python3 sendEmail.py ' + subject + " " + body);
    }catch(error){
        console.error(error);
    }
});

在客户端。

<script>
async function notifyOwner(ele) {
  const name = document.getElementById("name");
  const context = document.getElementById("context");
  await fetch("api/notify", {
    method: "POST",
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({sub: name.value, cont: context.value})
  }).catch(console.log);
}
</script>
<button type="submit" class="btn" onclick="notifyOwner(this)">Submit</button>

【讨论】:

    【解决方案2】:

    在客户端脚本标签中:

    async function notifyOwner(ele) {
      const form = ele.parentElement.parentElement
      await fetch("api/notify", { 
        method: "POST",
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({data form.whatever}) // Replace whatever with whatever data you want to send from the form
      }).catch(console.log);
    }
    

    HTML:

    ...
            <button type="submit" class="btn" onclick="notifyOwner(this)">Submit</button>
    ...
    

    this 传递给notifyOwner

    在服务器中:

    app.use(express.json());
    
    app.post("/api/notify", async (req, res) => {
      try{
        const { data } = req.body; // Get key that was sent
      }catch(error){
        console.error(error);
      }
    });
    

    【讨论】:

    • 是否需要安装任何库才能使用 bodyParser?
    • 这是express.json() 不是express.bodyparser() 抱歉。不,如果您使用的是 Express 4.16+。
    • var 主题为空
    • 尝试在notifyOwner中登录ele.parentElement.parentElement.getAttribute('id'),看看它是否真的返回了id
    • 对不起,我有一个错字。我的 var 主题正在返回“myForm”,这是包含表单的 div 的 id,这不是我需要的。
    猜你喜欢
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 2017-01-06
    • 1970-01-01
    相关资源
    最近更新 更多