I have a table with user data. I also have a tab where an admin can add new users. It works but I have to reload the page to see the new row. Of course, I can manually rewrite HTML in my event handler
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Admin Page</title>
</head>
<body>
<div class="container">
<div class="row">
<table class="table table-hover text-center">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Last name</th>
<th scope="col">Department</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>IT</td>
</tr>
<tr>
<td>Jane</td>
<td>Doe</td>
<td>HR</td>
</tr>
</tbody>
</table>
<a href="#" class="btn btn-outline-primary" data-toggle="modal" data-target="#add-new-user">Add new user</a>
<div class="modal" id="add-new-user">
<div class="modal-dialog modal-centered">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title text-center w-100">Fill in the forms</h3>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="name">Name: </label>
<input id="name" name="name" type="text" class="form-control" />
</div>
<div class="form-group">
<label for="last-name">Last name: </label>
<input id="last-name" name="last-name" type="text" class="form-control" />
</div>
<div class="form-group">
<label for="department">Department: </label>
<select id="department" name="department" class="form-control">
<option value="IT">IT</option>
<option value="HR">HR</option>
<option value="accounting">Accounting</option>
</select>
</div>
<input type="submit" class="btn btn-primary d-flex ml-auto" value="Submit" />
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
$(document).ready(function () {
$('form').on('submit', async function (event) {
event.preventDefault();
$('tbody').append(
`<tr>
<td>
${$(this).find('[name=name]').val()}
</td>
<td>
${$(this).find('[name=last-name]').val()}
</td>
<td>
${$(this).find('[name=department]').val()}
</td>
</tr>`
);
$(this).closest('.modal').modal('hide');
});
});
</script>
</body>
</html>
but at any rate I don't think it's best practice and it's not very practical if you have rows with lots of data. My rows also have modals within them so you imagine what a pain it would be
In the actual project, the data in the table is pulled from a database
How do (or rather, should) I dynamically change the page content without page (re)loading (or, preferably, manual rewriting of my HTML in event handlers)?
This SO page doesn't answer my question. I examined all suggested "similar" questions so if it turns out to be a duplicate, it's not because I was lazy. Please keep it in mind