使用node.js

时间:2017-01-12 22:06:36

标签: javascript jquery ajax node.js server

我需要在表单提交时显示一些加载gif 但是,在表单提交时不刷新页面, 只是显示一个谢谢!消息:

目前,它通过提交刷新页面,我不想刷新只在提交后显示消息。

以下是代码:

 /* form hbs */
 <form id="myForm" style="display:block" method="POST" role="form" class="form col-md-4 col-md-offset-4 col-xs-12">
                <div class="form-group">
                    <input type="text" class="form-control" id="name" name="name" placeholder="Your Name...">
                </div>
                <div class="form-group">
                    <input type="email" class="form-control" id="email" name="email" placeholder="Email...">
                </div>
                <div class="form-group">
                    <input type="text" class="form-control" id="companyName" name="companyName" placeholder="Your Company Name...">
                </div>
                <div class="form-group">
                    <input type="text" class="form-control" id="message" name="message" placeholder="Your Message Here...">
                </div>
                {{!--onClick="hide();"--}}
                <button type="submit" class="btn btn-primary" id="hide">Submit</button>
 </form>



 //in route index.js
 /* GET Add Contact Us page. */
 router.get('/ContactUs', function (req, res, next) {
 res.render('ContactUs', {
 title: 'Contact Us'
   });
 });

router.post('/ContactUs', function (req, res) {
var name = req.body.name;
var email = req.body.email;
var companyName = req.body.companyName;
var message = req.body.message;

var SQL = "INSERT INTO Contacts(name, email, companyName, message) VALUES($1, $2, $3, $4)";
//send the message to email
handleSayHello(req, res);
query(SQL, [name, email, companyName,message], res, function (json) {

res.render('ContactUs', {
   title: 'Success!'
     });
   });
});


function query(sql, arr, hbsResponse, listener) {
pg.connect(process.env.DATABASE_URL, function (err, client, done) {
if (err) {
  return hbsResponse.render('error', {
    error: err,
    message: err.message
  });
}

client.query(sql, arr, function (err, result) {
  done(); //close the connection
  if (err) {
    return hbsResponse.render('error', {
      error: err,
      message: err.message
    });
  }

  listener(result);
    });
  });
}

如何通过发布请求的ajax实现它而不刷新页面并隐藏表单(而是在提交后显示另一个div?

提前感谢谁能帮助我,我将非常感激!

0 个答案:

没有答案