Simple way to check if form elements have changed

Discussion in 'Javascript' started by rojelio@gmail.com, Oct 12, 2007.

  1. Guest

    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.
    , Oct 12, 2007
    #1
    1. Advertising

  2. Evertjan. Guest

    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.



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Oct 12, 2007
    #2
    1. Advertising

  3. xprotocol Guest

    On Oct 12, 11:34 am, wrote:
    > 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
    xprotocol, Oct 12, 2007
    #3
  4. 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.
    drink.the.koolaid, Oct 12, 2007
    #4
  5. Guest

    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
    , Oct 12, 2007
    #5
  6. RobG Guest

    wrote:
    > 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".



    --
    Rob
    "We shall not cease from exploration, and the end of all our
    exploring will be to arrive where we started and know the
    place for the first time." -- T. S. Eliot
    RobG, Oct 13, 2007
    #6
  7. wrote:
    > 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.
    Richard Cornford, Oct 14, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. magister

    Check Form controls have changed

    magister, Feb 4, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    619
    yurps
    Feb 4, 2005
  2. Replies:
    1
    Views:
    664
    Rosanne
    Oct 11, 2005
  3. Taras_96
    Replies:
    19
    Views:
    224
    Taras_96
    May 18, 2006
  4. Angel
    Replies:
    4
    Views:
    113
    Angel
    Jul 17, 2006
  5. mxbrunet
    Replies:
    1
    Views:
    195
Loading...

Share This Page