简体   繁体   中英

How to pass a value from HTML to Node JS?

I tried to get a value from a HTML table to Node JS. I tried like following,

HTML

<table class="table">
   {{#images}}
   <tr>
       <td><a href="/deleteImage" class="btn btn-danger" title="Clear All">X</a><label value={{imgURL}} name="imageID">{{imgURL}}</label>
       </td>
       <td><img src={{imgURL}} alt="Mountain View" style="width:228px;height:340px"></td>
   </tr>
   {{/images}}
</table>

app.js

app.get('/deleteImage', function (req, res) {
  images.removeImage(req.body.imageID, function (err, result) {
    if (err) return res.json(err);
    images.getImages(function (err, images) {
        if (err) return res.json(err);
        var msg = 'Deleted ' + result.affectedRows + ' rows.';
        res.render('delete_image.html', {images: images, msg: msg});
    });
  });
});

I tried to read the imageID from the HTML like above. But It's undefined . How can I fix this.

Thanks in Advance!

you can try

<a href="/deleteImage?imageID={{imgURL}}" class="btn btn-danger" title="Clear All">X</a>

and use req.query.imageID instead

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