0
votes

I have two ejs in my views folder I have created very simple ejs to see if I can send the variable from one ejs to another ejs.

a.ejs in veiws file

<form name="input" action="\home" method="post">
      <input type='checkbox' checked>Cheese
    <input type="submit" value="Submit" href="validation.ejs">
    </form>

b.ejs has

<h1><% post my variable here%></h1>

in my node js this is what i did const express = require('express'); const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.urlencoded({ extended: false }));

app.set('view engine', 'ejs');

app.get('/', (req, res) => {
    res.render('index', { title: 'EJS Demo' });
});

app.post('/', (req, res) => {
    const a = req.body.a;
    res.get('index2',a);
});

app.listen(3000, () => {
    console.log('Listening....');
});

i think the post has to do something in here...

1

1 Answers

0
votes

I think you need to submit a value from form1 in page1 and pass that variable to page2

if thats the case you can do like this

const express = require('express'); 
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.urlencoded({ extended: false }));

app.set('view engine', 'ejs');


// send the page1 to the user
app.get('/', (req, res) => {
    res.render('page1', { title: 'EJS Demo' });
});


//capture the submit from the user and then send another page
//to the user
app.post('/submitForm', (req, res) => {
    const a = req.body.valueFromA;
    res.render('page2',a);
});

app.listen(3000, () => {
    console.log('Listening....');
});
<form name="input" action="\home" method="post">
      <input type='checkbox' checked>Cheese
    <input type="submit" value="Submit" href="validation.ejs">
</form>

<!-- page1 form1 -->

<html>
<head>
  <title> <& title &> </title>
</head>
  <form action='/submitForm' method='post'>
    <input type='text' value='' name='valueFromA' />
    <input type='submit' />
  </form>
</html>


<!-- page2 ejs -->

<html>
  <body>
    <p> <% value %> </p>
  </body>
</html>

b.ejs has

<h1><% post my variable here%></h1>
in my node js this is what i did const