I am building an express app. I have a drop-down whose submitted value is passed to users.js and initiates a mongodb query. Then, result of that query is passed back to ejs and becomes an argument of a script function.
The problem is I want that function to be called when I click on Submit. (I can afford the delay in rendering of page).
Right now, I have two buttons, one for submit and one for calling that function.
index2.ejs
<form action="/users/details" method="POST">
<fieldset>
<select required name="selectpicker">
<!-- options -->
</select>
<input type="submit" id="submit-form" />
</fieldset>
</form>
<button onclick="popu(<%= JSON.stringify(coun) %>);">blue</button>
and users.js
router.get("/index2", function(req, res, next) {
(async function() {
count = await funcoun(coll); // a mongodb query
res.render("index2", {title: 'some data', userdataa: a, imgsrc : coll, coun : count});
}) ();
});
router.post("/details", function(req, res, next) {
coll = req.body.selectpicker;
res.redirect("/users/index2");
})
So, basically I want the form to pass value (of coll) to express, calculation of count, calling of function with this value, and then rendering the page.
Currently, calculation of count is done on basis of old value, and when blue button is clicked then present value.
I am confused that if I put onclick in submit button, what will it do since coun is not yet calculated.
CodePudding user response:
<body onl oad = "popu(<%= JSON.stringify(coun) %>);">
solved the issue
