[ acky.net logo ]

Static Networks Internet Services
Static Networks Internet Services
Home > Tutorials > JavaScripts > Save Form Data With Cookies

Save Form Data With Cookies
By Alex Osipov
LAST EDITED: Monday, July 23, 2001 6:35 PM

   JavaScript as a flexible language can help you make your life and the lives of your users easier. By using client-side JavaScript you can create different ways of saving form data, and thus make everyone's job easier. Take for example how much your users will appreciate not having to re-enter user information every time they visit your web site. Well with the use of JavaScript and cookies you can do just that.

    The first thing that you will need to do is create a simple html document and pre-load the external JavaScript that will handle saving and retrieving the form data. To do this you must place

<script language="JavaScript" src="cookies.js">
</script>

inside the head element. You also have to add

onLoad='retrieve_field(document.formname.fieldname);
retrieve_field(document.formname.fieldname);...'

to the body element and enter the fields that you want to be retrieved by replacing 'this.formname.fieldname' with the appropriate fields. Saving and retrieving data is extremely simplified by two functions, save_field() and retrieve_field(), which you can use without having to get into the gory and technical details of the actual data handling.

If you would like a certain field to be saved all you have to do is add

save_field(this);

to any event handler in an input element. For example:

<input type="text" name="username" onChange="save_field(this)">

To save a field on form submit you can simply add

save_field(this.fieldname);save_field(this.fieldname2);...

to the onSubmit handler of the form.

    That's all there is too it if you don't want to get into the details of how the JavaScript actually saves and retrieves the cookie. You can view a simple example of a page using this at example.htm, and you'll also need cookies.js. Don't forget you must put both files in the same directory or change the src property when loading the JavaScript.

    Now its time to get into the details. If you are squeamish and don't want to or need to know how the JavaScript actually does the work, I suggest you leave off here. However, if you have the guts to face understanding the JavaScript then...

 

 

Email This Page To A Friend

Last updated: Monday, November 27, 2006 - 11:02 AM Eastern Daylight Time
Legal | Privacy Statement | Problems & Questions | Advertise | Link to us
© 1997-2007, All Rights Reserved.


Dedicated Server Provided by HighSpeedHosting