i'm new to node js and express. I need to refresh only a portion of page (a td in table or the whole table) after perfoming a database update query using express. So in this way i'll show the updated value for the td without refreshing the whole page. The update query is performed by click an <a> element. Thank you.
Here some code i use:
Html from index.ejs page:
<thead>
<tr><th>Goals</th></tr>
</thead>
<tbody>
<%
if(userData.length!=0){
var i=1;
userData.forEach(function(data){
%>
<tr>
<td><a href="add-goal?id=<%= data.id %>"><i class="bi bi-plus-circle-fill text-success"></i></a><span class="badge bg-primary rounded-pill"><%=data.goals%></span></td>
</tr>
<% i++; }) %>
<% } else{ %>
<tr>
<td colspan="10">No Data Found</td>
</tr>
<% } %>
<tbody>
Select Query to database:
router.get('/players-list', (req, res, next) => {
db.query('SELECT * FROM players_list ORDER BY goals DESC;', (err, data, fields) => {
if (err) throw err;
});
});
Update Query to database:
router.get('/add-goal', (req, res, next) => {
db.query('UPDATE players-list SET goals = goals + 1 WHERE id = ?', req.query.id, (err, data) => {
res.redirect('players-list');
});
});
P.S. It doesn't make much difference if i refresh the td, the tr or the whole table.