0

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});
        }
    });
    };

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

it's a nice step forward, if i'm adding <%= scan.url %> insted of {{currentPhotoId} and i'm console logging here: exports.xray = function(req, res){ if(req.query.id && req.query.status) { // UPDATE QUERY WHERE ID = req.query.id and STATUS = req.query.status console.log(req.query.id) console.log(req.query.status) } i'm getting the id and the status sent, lets say 4 Rejected now let me try to send it back

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.