Simple way to check if form elements have changed

R

rojelio

I have a form with many fields... the fields are getting values from a
database.

I've tried onunload to just submit the form regardless of changes or
not but onunload and submit isn't working for me. Now I need some
type of event that will fire when a user tries to navigate from a page
to check to see if anything has changed and if so prompt them
(confirmation) to take action or not.

I know that the reset button only takes away anything that was added
to fields after the load so there must be some way to use the logic of
reset to simply check to see if fields have changed, right?

I need some help on this thanks.
 
E

Evertjan.

wrote on 12 okt 2007 in comp.lang.javascript:
I have a form with many fields... the fields are getting values from a
database.

I've tried onunload to just submit the form regardless of changes or
not but onunload and submit isn't working for me. Now I need some
type of event that will fire when a user tries to navigate from a page
to check to see if anything has changed and if so prompt them
(confirmation) to take action or not.

Wrong idea to trust onunload and alike.

"navigating from a page" can mean tripping over the mains cord,
or pressing ctrl-alt-del.

If you want to secure changes in the page on a serverside database,
do so directly by AJAX or by refreshing[+formpost] the page.
I know that the reset button only takes away anything that was added
to fields after the load so there must be some way to use the logic of
reset to simply check to see if fields have changed, right?

Ever tried this, btw?

<input
value='myDefault'
onchange=
'if (this.value!=this.defaultValue)
alert("Changed")'>

effect appears only on blurr.

I need some help on this

Could never have guessed that.
 
X

xprotocol

I have a form with many fields... the fields are getting values from a
database.

I've tried onunload to just submit the form regardless of changes or
not but onunload and submit isn't working for me. Now I need some
type of event that will fire when a user tries to navigate from a page
to check to see if anything has changed and if so prompt them
(confirmation) to take action or not.

I know that the reset button only takes away anything that was added
to fields after the load so there must be some way to use the logic of
reset to simply check to see if fields have changed, right?

I need some help on this thanks.

Never tried it but according to the MSDN, input elements have a
defaultValue property. This might help you out:
http://msdn2.microsoft.com/en-us/library/ms535841.aspx
 
D

drink.the.koolaid

You could have a script that:
* attached a function to the onchange event for each input...
* The function could add to an array the id of the changed
input.
* Later, you could check the array's length to see if any
data had changed.
* If it had, use docuemnt.getElemenyById and the ids in the
array to gather the data from each input.

For this kind of excercise, the usual concern applies: none of this
code will work if javascript is disabled.
 
R

rojelio

I've gotten it to work.... the submit onunload event. It's what the
client wants so mpmh
Solution I couldn't have the submit button named submit... go figure


-rlr
 
R

RobG

I've gotten it to work.... the submit onunload event. It's what the
client wants so mpmh
Solution I couldn't have the submit button named submit... go figure

It masks the form's submit method. You should never name a sumbit
button "submit".
 
R

Richard Cornford

I've gotten it to work.... the submit onunload event. It's
what the client wants so mpmh

Be very careful with this. If a page is navigating you may get away with
triggering a submit from an unload event and so effectively cancel the
navigation. But if a browser is being closed down it may still trigger
an unload event but may decide that there is no point in making any
additional HTTP requests (so no point in attempting to submit anything).
If you effectively train the users to expect their changes to be saved
automatically when they navigate they will also gain the expectation
that their changes will be saved automatically when they close the
browser down. If you cannot satisfy that expectation then you should not
encourage it.
Solution I couldn't have the submit button named
submit... go figure

And because you decided to keep your code to yourself it was not
possible for anyone to point out that very common mistake:-

<URL: http://jibbering.com/faq/faq_notes/form_access.html#faComMis >

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top