Anatsu
Anatsu

Reputation: 364

post a mySQL value return using nodeJS express and EJS

i have a mysql table looking like this

  id  photo_name  mob_no Status      url
   4   one          3434  OK          http://foofo
   5   two          434   Rejected    http://barbar
   6   three         434   Pending     http://bazbaz

i'm printing it's values with ejs file but i would like to present 3 buttons which will allow to change the status to: Approve,Reject or pending lets say it'll return the query:

        UPDATE `scans` SET `status` = 'OK' WHERE `id` = 6

unfortunately i couldn't find a way to use the post method and return these values only by clinking a button. iv'e tried to use form with a post method but it didn't worked, i also don't want to fill in a form with text, just press a button that basicly returns a string.

i'm printing the table like this:

      app.get('/home/xray', user.xray)
  exports.xray = function(req, res){
  db.query('SELECT * FROM scans', function (err, result) {
        if (err) {
            throw err;
        } else {

            res.render('xray.ejs', {result});
        }
    });
    };

Upvotes: 0

Views: 198

Answers (1)

Samuel Roberto
Samuel Roberto

Reputation: 461

You can send two params to the same route by query string. Follow this steps:

NodeJS Route:

app.get('/home/xray', user.xray)
  exports.xray = function(req, res){
  if(req.query.id && req.query.status) {
    // UPDATE QUERY WHERE ID = req.query.id and STATUS = req.query.status
  }
  db.query('SELECT * FROM scans', function (err, result) {
    if (err) {
      throw err;
    } else {
      res.render('xray.ejs', {result});
    }
  });
};

Template EJS:

<a href="?id={{currentPhotoId}}&status=OK">Status Ok</a>
<a href="?id={{currentPhotoId}}&status=Rejected">Status Rejected</a>
<a href="?id={{currentPhotoId}}&status=Pending">Status Pending</a>

To be sure and improve security you can check the status passed via query string, but this should work.

Upvotes: 1

Related Questions