简体   繁体   中英

Can I use 2 POST requests on click of one button using html, mysql and NODE.js?

I want to save form details to MYSQL database and also mail the form details to admin using nodemailer. So far I am able to send data to admin and save data to MySql database individually but what I want it to do is to save and send form details simultaneously.

    app.post('/send',function(req,res){

        var type_lea=req.body.lev_type.value
        res.write('type "' + req.body.lev_type.value+'".\n');
      let transporter = nodeMailer.createTransport({
          host: 'smtp.gmail.com',
          port: 465,
          secure: true,
          auth: {
              user: 'marcus1313@gmail.com',
              pass: 'Password'
          }
      });

    app.post('/submit',function(req,res){

      var employ_id=req.body.emp_id;
    var reason=req.body.leave_reason;
    var sql = "INSERT INTO leave_det (Emp_id,Reason) VALUES                                         
    ('"+req.body.emp_id+"','"+         req.body.leave_reason+"',)";
        conn.query(sql, function (err, result) {
          if (err) throw err;
          console.log("1 record inserted");

        });

in this html code I am able to call only one post request but I want to call them both at the same time.

    <form action="/send" name="mail"  class="loyal" method="post" >
   </form>

You can only send your POST request to one single endpoint. But this should not be an issue here as you may call your mailing code within the same function where you store the data to the database.

As the call to the SMTP server will take some time, I would move the mailing task completely out of the loop here. Just write to the database and do the mailing in a seperate background processing job.

Please also check your SQL statement carefully for risks of SQL injection.

You can try using this method may be it will help you

var app = express()

function otherPath(req, res, next) {
    return res.send('ok')
}

function home(req, res, next) {
    req.url = '/other/path'
  /* Uncomment the next line if you want to change the method 
    req.method = 'POST'
  */
    return app._router.handle(req, res, next)
}
app.get('/other/path', otherPath)
app.get('/', home)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM