提前谢谢大家!真的>
DIV中有一个用于撰写电子邮件的表格。用户输入收件人,标题和内容,然后单击提交。表单可以提交。
在html中:
<form id='formid' name='compose1' action='/composemail' method='post'>
New Message
<p>To: <input type='textbox' name='recipient'></p>
<p>Subject: <input type='textbox' name='title'></p>
<p><input type='textarea' name='content' class='textarea'></p>
<input type='hidden' id='formTime' name='time' value='20:00:01 Wed Nov 06 2019'>
<input type='submit' id="formSubmit" value='Send'>
</form>
单击提交后,数据将插入到mongo数据库中。数据可以成功添加。 在app.js中:
app.post('/composemail',express.urlencoded({extended:true}),function(req,res) {
var db = req.db;
var collection = db.get('emailList');
var sender = "abc@mail.com";
var recipient = req.body.recipient;
var title = req.body.title;
var time = req.body.time;
var content = req.body.content;
var mailbox = "Sent";
var newmail = {'sender':sender,'recipient':recipient,'title':title,'time':time,'content':content,'mailbox':mailbox};
collection.insert(newmail,function(err,result) {
if (err === null) {
console.log("Successfully added!");
res.json({success: true}); // <-problem here
} else res.send(err);
})
})
在另一个文件script.js中,有一个函数调用show
function show() {
//basically a XMLHttpRequest to receive the email list from the database
}
问题是插入数据后的响应。我希望它保留在同一页面上,并在提交表单后在script.js中运行show()函数。
我在互联网上搜索,发现这可能有用:res.json({success:true}); 但我想避免使用Ajax,因为它确实很复杂... 如何实现成功为真,然后以html的简单形式运行show()函数?我尝试过:
<form id='formid' name='compose1' action='/composemail' method='post' success='show(variable)'>
它似乎不起作用...或者如果我真的必须使用Ajax,我该怎么办?我真的不知道...
非常感谢您!