I am making a website in which I need to get information from a form and store it in a table that everyone else can see.
For example, every time someone fills out the form and presses "submit", the user is taken to another page where a table is created. The table will append the information that the user just put. If the tab is refreshed or someone else visits the site, the information in the table will still be there.
I believe I need a server for this, so if there is any guidance regarding how to use a server, it would be much appreciated. -THERE ARE COMMENTS REGARDING THIS, NOW I NEED AN ANSWER TO THE QUESTION BELOW
However, more specifically, I want to at least be able to use local storage to find out the technique for saving the data and keeping it in the table for at least one user.
I have some code below that is attempting to use local storage, but it is not currently working. I have a form elsewhere, and I also call the div myresult in another page. Any help is much appreciated. Thanks!
function refresh() {
const result = document.getElementById("myresult");
const saved = JSON.parse(localStorage.getItem("save"));
if (saved) {
const t =
`<table>
<tr>
<th>Name</th>
<th>LastName</th>
</tr>
<tr>
<td>${saved.name}</td>
<td>${saved.lastname}</td>
</tr>
</table>`;
myresult.innerHTML = t;
}
const form = document.getElementById("myform");
form.addEventListener("submit", e => {
e.preventDefault();
const {
elements
} = form;
const output = {
name: elements.name.value,
lastname: elements.lastname.value,
};
localStorage.setItem("save", JSON.stringify(output));
const template =
`<table>
<tr>
<th>Name</th>
<th>LastName</th>
</tr>
<tr>
<td>${output.name}</td>
<td>${output.lastname}</td>
</tr>
</table>`;
myresult.innerHTML = template;
})
}