SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How's it going?

    Does anybody please know how to create a variable in Javascript that is created only when a user has edited one of the fields in a form (can assume all form types are text areas).

    I'd then like to be able to gain access to this variable on the next page (i.e. after the form button is pressed), so that this variable can tell me whether or not the user has edited any of the form details.

    Then depending upon the value of this variable, the records are updated in the database.

    Please note, all the database stuff is being done with PHP, I'd just like to know how to create the variable, if possible.

    Many thanks if you can help,

    Regards,

    Jason

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Add a hidden form field, like this:

    <INPUT TYPE=HIDDEN NAME="FormChanged" VALUE="0">

    Then, whenever one of the form fields is changed, you should set the value of the hidden field to true:

    <INPUT TYPE=TEXT NAME="SampleField" onChange="this.form.elements['FormChanged'].value=1">

    That should do it! But realize that in so doing you will be blocking anyone without JavaScript support in their browser from using your form.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  3. #3
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi BackPack,

    While Kevin's solution is what you asked for, it raised a question of my own: why are you updating the entire record if only one field is changed?

    In other words:
    A boolean value will only tell you that something has changed,not what has changed. I presume you don't want excessive server usage and that is why you are using the boolean. But, if your form reflects a record extracted from 2 or more tables, you will have to update all of the reords involved for all of the tables == even if the change was of no consequence (a change that then was rechanged to he original).

    My suggestion would be to create an array of elementNames & values with each change and then access that array from the second page with either the method Kevin demonstrated or the opener property to determine whether or not the dbms should be updated and for which specific columns.

    <input .....onChange="fillArray(elementname,this.value)">

    var changedFields = new Array();
    fillArray(elName,elVal)
    {
    i = changedFields.length;
    changedFields[i++] = elName;
    changedFields[i] = elVal;
    }

    since the values will always be in the odd cells and the names in the even cells, you can use the mod operator to identify the two easily.

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •