Jenan
Jenan

Reputation: 3320

node.js - express - send data - form

I use node.js - Express. I have a page (index.html) and html div (id = box) and input (id = inputUser). I want to express through insert (when the page loads) text into the div and input.

Thanks for the advice!

Client:

<form method="post" action="/">
    <input type="text" name="inputUser" id="inputUser" />
    <div id="box"></div>
</form>

Server:

app.get('/', function (req, res) {
  res.render('index.html', { layout: false });
});

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

Upvotes: 0

Views: 2519

Answers (1)

alessioalex
alessioalex

Reputation: 63663

From what I see in your code snippet above, you are probably using EJS as the view engine. You can pass the variables to the view just like you pass the false option for layout.

Server

app.get('/', function (req, res) {
  functionToLoadDataFromDatabase(function(divData, userData) {
    // this is the callback
    res.render('index.html', { 
      layout: false,
      divData: divData,
      userData: userData
    });
  });
});

Client-side

<form method="post" action="/">
  <input type="text" name="inputUser" id="inputUser" value="<%= userData %>" />
  <div id="box"><%- divData %></div>
</form>

From the EJS documentation:

Escapes html by default with <%= code %>   
Unescaped buffering with <%- code %>

Upvotes: 2

Related Questions