问题描述
我创建了一个将数据发布到MongoDB数据库的表单。问题是我发帖时希望刷新输入字段而不必刷新整个页面。
当我调用函数resetBooks()时,我认为它会先清除表单,然后再不发布任何内容,对吗?
如何获取表格以发布数据,然后清除输入字段。
<div class="booksInput">
<h4>Add a book</h4>
<form id="bookForm" method="post" action="/books"
target="hiddenFrame" onsubmit="resetBooks()">
<input class="form-control" name="title" type="text" placeholder="book title">
<input class="form-control" name="author" type="text" placeholder="author">
<input class="form-control" name="description" type="text" placeholder="short description">
<button class="btn btn-primary" type="submit" value="Add Book">Submit</button>
</form>
</div>
<script>
function resetBooks(){
document.getElementById('bookForm').reset();
};
app.post("/",(req,res)=>{
let myBook = new Book (req.body);
myBook.save()
.then(item =>{
res.send("Book saved to database");
})
.catch(err=>{
res.status(err).send("unable to save to database");
});
});
</script>