javascript - Fire on return from form "Submit" -
i want able parse values on return submit post. can't seem find way without manually initiating ajax call through javascript
code on webpage:
<form id="form1" method="post" action="/formsubmit" > <input type="text" name="username"> <input type="text" name="email"> <input type="submit" value="submit"> </form>
code on nodejs server:
router.post('/', function(req, res) { console.log("here: " + req.body.username); console.log("here: " + req.body.email); res.json({ data1: 'hello', data2: 'world'}); }
edit #1: renders "{ data1: 'hello', data2: 'world'}" browser, need parse it
what want see after form submit? way handle post request you're sending json. indeed need ajax render result more beautiful. in order avoid usage of ajax have send response html file.
of course can pass submitted form data template , send user. pass variables need template:
res.render('formsubmit', {data1: 'hello', data2: 'world'});
for need have template file in template directory called formsubmit
(plus file ending depending on view engine) takes given variables. express take care of creating html file out of it.
e.g. handlebars can access variables in template file named formsubmit.hbs
in views folder like:
<p>hello, name {{data}} , not {{data2}}.
see also: https://expressjs.com/en/guide/using-template-engines.html
Comments
Post a Comment