Eogcloud
Eogcloud

Reputation: 1365

Accessing posted form data in node/express

I am working on a small JS application using node and expressto graph user data that is input with a form and then posted.

So far I've got my post method for that page as follows to capture the data.

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

    var barValues = req.body.myInputs;
    var labelValues = req.body.myLabel;

    console.dir(barValues);
    console.dir(labelValues);
});

My problem is that I want to now render a new blank page that I'll use the draw and render the graph and I need the above data. will this data be available in the "res" of the next page? How can I move or put the data somewhere so that I can access it easily with client side JS to draw stuff with canvas and graph the input?

Upvotes: 1

Views: 145

Answers (1)

Hector Correa
Hector Correa

Reputation: 26680

The data that you posted to /barchartentry won't be automatically available on the next page that you render, but you can easily pass it to the next page:

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

    var barValues = req.body.myInputs;
    var labelValues = req.body.myLabel;

    console.dir(barValues);
    console.dir(labelValues);

    // pass an object to the view to render with the data that you need
    data = {title: 'my chart', values: barValues, labels: labelValues);
    res.render('chartview', data);

});

In your view (chartView in the example) you'll have access to those values as you render the HTML. For example if you are using EJS as your template for view you can do something like this:

<h1><%= title %></h1>

<% for (var i = 0; i < values.length; i++) { 
    value: <%= values[i] %>
} %>

Upvotes: 1

Related Questions