Abhinav
Abhinav

Reputation: 1027

NodeJS, express: post data from one page to another

I have to post some data from one page to another page. I am using express with NodeJS.

I have written below code :

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

app.get('/', function (req, res) {
   res.sendFile( __dirname + "/index.html" );
});

app.get('/RegistrationPage', function (req, res) {
   res.sendFile( __dirname + "/RegistrationPage.html" );
});

app.post('/RegistrationSuccessPage', function (req, res) {
   var uname = req.body.username
   var pwd = req.body.pwd
   var emailAddress = req.body.email
   postData = uname+","+pwd+","+emailAddress
   console.log(postData);
   res.sendFile( __dirname + "/RegistrationSuccessPage.html",uname);
});

On navigating to "RegistrationSuccessPage", the values of uname, pwd, emailAddress are being printed as undefined.

I have used below code too but it did not helped:

app.post('/RegistrationSuccessPage', function (req, res) {
   var uname = req.query.username
   var pwd = req.query.pwd
   var emailAddress = req.query.email
   postData = uname+","+pwd+","+emailAddress
   console.log(postData);
   res.sendFile( __dirname + "/RegistrationSuccessPage.html",uname);
});

Can someone please help that how I can get values from text boxes and post them on other page.

HTML code is as below:

<body>
        <div>   
            <div align="center">
                <label>Please enter below details</label><br><br>
                <label>Username *: </label><input type="username" name="username"/><br><br>
                <label>Password *: </label><input type="password" name="pwd" /><br><br>         
                <label>Email Address *: </label><input type="email" name="email"><br><br>
                <br><br>
                <form action="/RegistrationSuccessPage" method="post"><input type="submit" value="Submit" /></form>
            </div>
        </div>
    </body> 

Upvotes: 0

Views: 5155

Answers (1)

Nalla Srinivas
Nalla Srinivas

Reputation: 933

You are placing the input fields inside of the form tag. use below code

  <body>
    <div>   
        <div align="center">
          <form action="/RegistrationSuccessPage" method="post">
            <label>Please enter below details</label><br><br>
            <label>Username *: </label><input type="username" name="username"/><br><br>
            <label>Password *: </label><input type="password" name="pwd" /><br><br>         
            <label>Email Address *: </label><input type="email" name="email"><br><br>
            <br><br>
            <input type="submit" value="Submit" /></form>
        </div>
    </div>
</body>

now try i may work

Upvotes: 1

Related Questions