0

I am using nodejs with express and ejs.

Every one on internet ask how to pass value from node to the view, but what about the opposite?

For example, I ask my user to input a string in a form, when the user clicks the button, how can I get this string without passing it as a parameter in the url?

The form:

<div class="container">
        <form style="width:50%">
            <div class="form-group">
                <label for="text">Name of the product</label>
                <input type="text" class="form-control" id="pName">
            </div>
            <div class="form-group">
                <label for="text">Reciever</label>
                <input type="text" class="form-control" id="reciever">
            </div>
            <div class="form-group">
                <label for="text">Location</label>
                <input type="text" class="form-control" id="location">
            </div>
            <!-- <div class="checkbox">
            <label><input type="checkbox"> Remember me</label>
        </div>-->
            <button type="submit" class="btn btn-default">Submit</button>
        </form>

The app.js

var express = require('express');
var app = express();

//ALL GLOBAL VARIABLES
var port = 8080;

app.get('/', function(req, res) {
    res.render('index.ejs');
});

app.get('/barcode', function(req,res) {
    res.render('barcode.ejs');
});
app.listen(port);

I know I can do this:

app.get('/url/:parameter', function(req.res) {
var foo = req.params.parameter;
}

But if I don't want to use the URL, is it possible to retrieve the data?

dhilt
  • 13,532
  • 6
  • 48
  • 67
Florian Castelain
  • 1,524
  • 12
  • 39

4 Answers4

5

Use POST as a method for your html form

<form action="/myapi" method="post">
    <input type="text" class="form-control" id="pName" name="pName">
    <button type="submit" class="btn btn-default">Submit</button>
</form>

And then handle the client form "action" with app.post on the back end

app.post('/myapi', function(req, res) {
  res.send('The pName is "' + req.body.pName + '".');
});
dhilt
  • 13,532
  • 6
  • 48
  • 67
2

You can use POST method instead of GET. You need to change the route in your Express to

app.post('/url', function(req.res))

and add a method on your form

 <form style="width:50%" method="POST">
1

If you use a POST request the parameters are not part of the URL. E.g.

app.post('/path', function(req, res) {
    ...
    //You can retrieve the parameters of the POST request here
]);

You'll need the body-parser module to be able to get the POST parameters. Here's an answer about how to get the POST parameters once you've set up the route.

Your form should have a method and and action:

<form action="/path" method="POST">...</form>
Cromulent
  • 78
  • 1
  • 2
  • 12
0

From your question, In general if you want to get a value from the unique id, you will store that value as a global variable. so you can easily get the current user and user related details.

Ambrose Jesuraj
  • 69
  • 1
  • 10