How do you use js to remember form answers? when I tried getElementById, it would say, null. Can anyone teach me the correct way to remember a form? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How do you use js to remember form answers? when I tried getElementById, it would say, null. Can anyone teach me the correct way to remember a form?

6th Oct 2016, 4:35 AM
Trey
4 Answers
+ 6
hey i learn web designing basics from w3schools it's very good site
25th Jan 2017, 3:27 PM
Prashanth Kumar
Prashanth Kumar - avatar
+ 3
Try this code: <html> <head> <script type="text/javascript"> function store(){ if (typeof(Storage) !== "undefined") { //Access field by id property localStorage.setItem("username", document.getElementById("username").value); } else { alert("Sorry, no local storage :(") } return true; } function restore(){ //access form field by name document.firstform.username.value = localStorage.getItem("username"); } </script> </head> <body onload="restore()"> <form name="firstform" method="post" action="#" onsubmit="return store()"> <input type="text" name="username" id="username" value="generated value"/> <input type="submit" value="Post form"/> </form> </body> </html> Notes: - you will notice that at first load the field contains "generated value", but after post, you will get the value entered back - body.onload will not be enough in production environment, as complex pages are just not ready directly after load completes
6th Oct 2016, 6:50 AM
Zoltán Zörgő
Zoltán Zörgő - avatar
0
Most modern browsers provide some feature to remember form values. But that is out of your control. You have two possibilities in your hands: server side and client side. The server side approach is the general one. In this scenario the user fills the form fields and posts it to the server. The server will validate the values and if it finds some mistake, the user will get the same form. I suppose you want to have the form pre-filled with the values the user entered. The solution is simple: as you have the user values, you can simply generate the html code to contain the values in the fields, as all html form fields are capable of this. But the "how to do this" depends on the tools you are using on server side. On client side you have two storage possibilities: cookies and the HTML5 local storage. But beware: cookies travel all the time from client to the server, thus it will be an overhead. The later is better, as it is most likely available, since really few are using older browsers nowadays. So, let's suppose, you want to use this approach. getElementById is working, for sure, but pay attention that you need to specify both name and id property for the field. Still, you can access the field by it's name also. But as you have not shared your code, it is impossible to see what you have done. More intel: http://www.w3schools.com/html/html5_webstorage.asp
6th Oct 2016, 6:48 AM
Zoltán Zörgő
Zoltán Zörgő - avatar
0
if you want the browser to remember the answer you probably talking about cookies. Most of times we use forms to send an email or take some basic information about a customer and we keep this information in databases to be secure, if you wanna do something like that a program language like php is needed. You can use javascript with php to take the informations from the form and then store them to your database.
7th Oct 2016, 2:03 PM
ΚΩΝΣΤΑΝΤΙΝΟΣ ΜΠΕΣΥΡΗΣ
ΚΩΝΣΤΑΝΤΙΝΟΣ ΜΠΕΣΥΡΗΣ - avatar