I am using the Node.js Cloudant DB Web Starter for a simple application that takes form input from a user and inserts it into the Cloudant DB, but I can't get control returned back to the form or another page. I am very new to this, so could very well be a lack of knowledge. When I enter information into the fields and hit submit, the data is captured from the fields and inserted into the cloudant DB, but then the browsers just displays "OK" and does not return to the form. What I want to do is return back to the same page.
**app.js**
var db = nano.use('contactme');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.post('/', function(req, res) {
// console.log(req.body);
res.sendStatus(200);
res.redirect('http://xyz.mybluemix.net/#top');
var form_name=req.body.name;
var form_email=req.body.email;
var form_message=req.body.message;
db.insert({"email": form_email,
"name": form_name,
"message": form_message}, null, function(err, body) {
if (!err){
console.log(body);
}
});
});
**index.html**
<p>If you would like to contact me please use this form below.</p>
<form method="post" action="/" >
<div class="row 50%">
<div class="6b"><input type="text" name="name" placeholder="Name" /></div>
<div class="6b"><input type="text" name="email" placeholder="Email" /></div>
</div>
<div class="row 50%">
<div class="12b">
<textarea name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12b">
<input type="submit" value="Send Message" />
</div>
</div>
</form>
Hope this helps. Your code has been in between a pseudo code and the real one. So I have kept only bare minimum things to demonstrate re-sending the form to the client.