Here is my code
<div class="input-box" id="form-box">
<form action="/" method="post" id="form" autocomplete="off" class="form-box" >
<h2>Paste Everything Here</h2>
<input type="text" name="all" id="all-input">
<h2>OR</h2>
<input type="text" name="time" id="time" placeholder="Time">
<input type="text" name="link" id="link" placeholder="https//zoom.link">
<input type="tel" minlength="10" maxlength="10" placeholder="Phone Number" name="phonenumber" id="phonenumber">
<input type="submit" value="Send Later" id="send-later" class="send" name="action" formtarget="_self">
<input type="submit" value="Send Now" id="send-now" class="send" name="action" formtarget="_blank">
</form>
</div>
<div id="list-container" class="input-box">
<ul>
<% if(sendList) { %>
<% console.log(sendList) %>
<% for(var i = 1; i < sendList.length; i++) { %>
<li class="ui segment">
<form action="/" method="post">
<input type="hidden" name="time" value= "<%= sendList[i].time %>">
<input type="hidden" name="phonenumber" value= "<%= sendList[i].phonenumber %>">
<input type="hidden" name="link" value= "<%= sendList[i].link %>">
<strong><%= sendList[i].phonenumber %></strong>
- <%= sendList[i].time %> <br>
<!-- <%= sendList[i].link %> -->
<input type="submit" value="Send" name="action" formtarget="_blank">
<button >Delete</button>
</form>
</li>
<% }; } %>
</ul>
</div>
This is Index.js file
var bodyParser = require('body-parser')
var urlencodedParser = bodyParser.urlencoded({ extended: true })
const cookieParser = require("cookie-parser");
const sessions = require('express-session');
// creating 24 hours from milliseconds
const oneDay = 1000 * 60 * 60 * 24;
//session middleware
app.use(sessions({
secret: "thisismysecrctekeyfhrgfgrfrty84fwir767",
saveUninitialized: true,
cookie: { maxAge: oneDay },
resave: false
}));
app.post("/" , urlencodedParser , (req , res) => {
if(req.body.action === "Send Now"){
console.log("in send now" + req.body.phonenumber);
console.log(req.body);
res.redirect(waMe.createFromNumberWithMessage("+91"+ req.body.phonenumber,
"Greetings! from support team \n" + req.body.link +
" \n\nAbove is the link for your free session with our expert counsellor \n\nYour session is scheduled for today at " + req.body.time));
}
if(req.body.action === "Send Later"){
console.log(req.session.index)
console.log(req.body);
if(req.session.index > 0){
req.session.index++;
console.log("im heree")
}
else {
req.session.index = 1;
req.session.sendlist = [];
console.log("im always here");
}
req.session.sendlist[req.session.index] = {time: req.body.time , link: req.body.link , phonenumber: req.body.phonenumber}
console.log(req.session.sendlist);
res.render("main" , {sendList: req.session.sendlist});
}
if(req.body.action === "Send"){
console.log(req.body);
res.redirect(waMe.createFromNumberWithMessage("+91"+req.body.phonenumber,
"Greetings! from support team \n" + req.body.link +
" \n\nAbove is the link for your free session with our expert counsellor \n\nYour session is scheduled for today at " + req.body.time + "\n\n It is starting soon! Join now."));
}
});
Here is the output:
I'm new to JavaScript and can't figure this out. The send now button used to work when I didn't have other submit buttons added.
As you can see, the req.body is unpopulated when I click send now and populated when I click send Later or Send Button in the <ul> <li>
list.