5

I'm working on a project where I'd like to split up a form over multiple pages.

We're currently using express for node.js.

Would it be possible to store the variable from one page and continue to pass it along to the subsequent pages?

The first page is called "signup" which directs to "instructions." The code snippit from instructions is as follows:

app.post('/instructions', function(req, res){
    res.render('instructions.ejs', {book-code=req.body.book-code, pages=req.body.pages, author=req.body.author, title=req.body.title});
});

The hidden form on the instructions.ejs page looks like:

<form action = "record" method="POST">
    <input type="hidden" name="iauthor" value="author">
    <input type="hidden" name="ipages" value="pages">
    <input type="hidden" name="ibook-code" value="book-code">
    <input type="hidden" name="ititle" values="title">
</form>

Then, the section for record from the ejs file is:

app.post('/record', function(req, res){
  pg.connect(process.env.DATABASE_URL, function(err, client, done) {
    client.query('INSERT INTO database(book, PAGES, NAME, TITLE) VALUES($1)', [req.body.ibook-code, req.body.ipages, req.body.iauthor, req.body.ititle], function(err, result) {

    if(err) return console.error(err);
    console.log(result.rows);
    });
  });
    res.render('record.ejs');
});
Katherine
  • 51
  • 2
  • just to make question clear for myself, why don't you try to store it in session? – Siavosh Mar 23 '14 at 15:27
  • 3
    From just a quick read of what you have above, that should work. Generally speaking, the answer to your first question (can you pass along form1's input to form2 and the do a final submit to the database based on form2) is "yes". @Siavosh asks a legitimate question about whether `req.session` could be used, but one might want to apply the approach when a single user (i.e. session) could be performing multiple operations (say in different windows), which the form-chaining approach would trivially support while doing it via session would be a bit more effort. – barry-johnson Mar 23 '14 at 15:30
  • 2
    @barry-johnson I was thinking just maybe Katherine is new to express and nodejs and is not aware of this possibility, if I'm wrong correct me – Siavosh Mar 23 '14 at 15:34
  • @Siavosh - just to be clear, I thought your suggestion was a good/valid one and suitable for a lot of use cases. I only wanted to mention the one scenario in which the form chaining would offer an easier path (because in most cases, session would be the easiest). Your comment popped up while I was writing and was already near max length, so kept the mention brief. – barry-johnson Mar 23 '14 at 15:39
  • @barry-johnson thanks yes I know that's why I voted up your comment and thanks for being informative – Siavosh Mar 23 '14 at 15:42

0 Answers0