0

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');
 }); 
}); 
3
  • You have to make an ajax request. Read about fetch Commented Jan 1, 2023 at 12:08
  • check this stackoverflow.com/questions/38179668/… Commented Jan 10, 2023 at 16:43
  • thanks for replying. I have used ajax to make it work. Commented Jan 16, 2023 at 11:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.