I am using Express.js with ejs template for my app. I have index.ejs file where I call a function getTableValue() on click of a button. This function get data from a table and save it in an array of objects name “arrs” which is declared inside this function. I want to send this “arrs” which is array of objects to my index.ejs file to insert the data in it to a MySQL table named “student” in database when user click on save. When I tried to run the code, I got an error “arrs is not defined”. How can I send arrs from ejs script to app.js.
how could I update my code with AJAX.
<button onclick=" getTableValue()">Get Table Value</button>
<a href="/assessResult/<% = arrs %> " class="btn btn-danger btn-sm">Save</a>
<script>
const arrs[];
function getTableValue( ){
var table = document.getElementById("display_excel_data");
var totalRows = document.getElementById("display_excel_data").rows.length;
for (var x = 1; x < totalRows; x++) {
const arr={};
arr["studentID"]=table.rows[x].cells[0].innerHTML;
arr["studentName"]=table.rows[x].cells[1].innerHTML;
arrs.push(arr);
}
}
</script>
app.get('/assessResult/:arrs, function(req, res){
var stID = req.body.studentID;
var stname=req.body.studentName;
var sql = 'insert into Student values(?,?);';
con.query(sql,[stID, stname], function(err, result, fields){
if(err) throw err;
res.redirect('/listcourse');
});
});