I want to use webstorage :sessionStorage for saving form records. When the user submits the form i have to print all the form values that were submited. Here is my code:
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm() {
//some checks here....
if(!sessionStorage.getItem("mainForm")) {
sessionStorage.setItem("mainForm", 0);
}
else {
mainForm = sessionStorage.getItem("mainForm");
}
mainForm++;
sessionStorage.setItem("mainForm",document.getElementById("id"));
var text = "";
var i;
for(i = 0; i < sessionStorage.length; i++) {
text += "Course id is" + i + "<br>";
}
document.getElementById("print").innerHTML = text;
}
</script>
</head>
<body>
<form name="mainForm" onsubmit="validateForm()">
<input type="text" name="id" maxlength="7" required>
<input type="submit" value="Register">
<p id="out" style="color:red"></p>
<p id="print"> </p>
</form>
</body>
</html>
The above code does not work. Can you explain me how to do it ?
Aucun commentaire:
Enregistrer un commentaire