0

I am trying to POST some values using my HTML form to a node JS method. Thereafter, I need to send the return result back from NODE JS and display it in the HTML page.

I am also using EJS to send back the values to the form. However, the result doesn't get displayed.

Basically, After the user clicks on the submit button on the HTML form, values should be passed to Node Js processed and send back a result Success or failed to the HTML doc where it'll display.

My code is as follows:

HTML CODE:

          <form id="form-register" action="http://localhost:8089/ttt""  method="post" enctype="multipart/form-data">

            <input type="logintext" value="" name="nam" class="nameC" id="mc" > 



             <input type="submit" name="subscribe" id="mc-submit-number">

             <label  >REEEEE <%= titles %></label>

          </form>

NODE JS CODE:

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

loginProvider.signUp(params, function(err, data) {
    if (err) {

      res.render('index',{title: 'HOME',titles:err.stack
      });
      res.send('WHATTT');
    }
    else   {
      console.log('Result ' + data);           // successful response
      res.render('index',{title: 'HOME',titles:'SUCCESS'
      });
      res.end('xzxzxzxzxzx');
    }  

});

}
Illep
  • 16,375
  • 46
  • 171
  • 302
  • Try adding body-parser, like mentioned [here](http://stackoverflow.com/questions/24800511/express-js-form-data). – flott Jul 07 '16 at 06:14
  • any specific errors ?...have you checked out browsers console/debugger for errors and moreover why do you need send() and end() ..the response is already being sent... – Nikhil Jul 07 '16 at 06:16
  • Already tried adding body-parser. Did not work. There are no error reported on Chrome Developer tools – Illep Jul 07 '16 at 06:23
  • However, I am seeing an error on node js that says `Error: Can't set headers after they are sent.` – Illep Jul 07 '16 at 06:26

2 Answers2

0

You could (or rather have to) use AJAX.

Then you can manually send your form parameters via post request to your Node server and return a response object with either an error or a success message.

0

The reason your code isn't working is because you have already loaded the DOM of the HTML page and when you are making that request the only way for EJS to work is if you reload the page since EJS just goes through your file and string replaces

My best solution is to have a normal AJAX call to your '/ttt' and have the Node.js do a 'res.send("SUCCESS");' and in your html you can set that label or any part of your document page to "SUCCESS"

With JQuery

$.post("/ttt", function(data, status){
   $("#yourLabel").text(data);
});
FrickeFresh
  • 1,688
  • 1
  • 19
  • 31