How to tell if a posted input value has been changed?

M

Martin

I have a form on a page in which there are 45 inputs of various kinds.
(It's a table with the user having the ability to update the displayed
data). In actual use, there will seldom be more than one or two inputs
that get changed at any given time. On the server side, the scripting
is having to do a LOT of work to figure out which inputs have been
changed and therefore need to be processed.

I'm wondering if there is a way that the server script can tell
whether a given input has been changed?

One idea I thought of is to use the javascript onChange event to
append some kind of a "flag" character to the input's value when the
user makes a change. Then, on the server side, I could check each
input value looking for that character and then, when found, strip it
off and do the updating. (and, yes, the page already has javascript
being used in it).

Anyone know of a better way?
 
J

Jukka K. Korpela

On the server side, the scripting
is having to do a LOT of work to figure out which inputs have been
changed and therefore need to be processed.

The programming work needed depends on the server-side technology used,
but it should be rather straighforward: use a loop that compares the
value of each input with its old stored value. For some technologies,
this might be easier if you name the inputs systematically, e.g. a1, a2,
a3, ...
I'm wondering if there is a way that the server script can tell
whether a given input has been changed?

The only reliable way is that the script compares each input value with
the value that was set as its initial value when creating the form.
One idea I thought of is to use the javascript onChange event to
append some kind of a "flag" character to the input's value when the
user makes a change. Then, on the server side, I could check each
input value looking for that character and then, when found, strip it
off and do the updating.

There are two fundamental flaws with the approach. First, it relies on
JavaScript, and in a particular bad way at that: the page might appear
to work with JavaScript turned off, but it would actually fail miserably
(no action would be taken server-side, as the server-side code would
think that nothing has changed). Second, it is pointless, since you
would still have to loop through all inputs - and you would need to do
extra operations both client-side and server-side, so this could easily
mean _more_ code and complexity than the robust approach.
(and, yes, the page already has javascript
being used in it).

Do you mean that the page has been written so that it does not work with
JavaScript? Then you already have a problem, but that's no reason to
make it worse.
 
B

Brian Cryer

Martin said:
I have a form on a page in which there are 45 inputs of various kinds.
(It's a table with the user having the ability to update the displayed
data). In actual use, there will seldom be more than one or two inputs
that get changed at any given time. On the server side, the scripting
is having to do a LOT of work to figure out which inputs have been
changed and therefore need to be processed.

I'm wondering if there is a way that the server script can tell
whether a given input has been changed?

If you were developing with ASP.NET then this is provided as part of the
framework - the state of controls is saved to the "viewstate" which is
basically a single hidden field which contains a serialized copy of the
previous state of all the controls on the page (or at least all the controls
which have "viewstate" enabled). That's one way of dealing with it. The
disadvantage is that the page is bigger because it contains copies of the
previous states and postbacks require more data to be sent to the server
because the previous state is going back to the server. Not a problem, just
something to be aware of.

An alternative would be to store the previous state at the server. If you
are say editing a database record then this approach makes sense because you
already have the "previous" state saved and available at the server. But for
other types of application it may not be appropriate to have a stored
snapshot on the server.
One idea I thought of is to use the javascript onChange event to
append some kind of a "flag" character to the input's value when the
user makes a change. Then, on the server side, I could check each
input value looking for that character and then, when found, strip it
off and do the updating. (and, yes, the page already has javascript
being used in it).

Yuck. Sorry, whilst this should work, the problem is that not everyone has
JavaScript enabled. Jukka has already given a better explanation of why then
I'd be able to.
Anyone know of a better way?

The viewstate approach works very well, but if I were developing in a
language which didn't have the equivalent built into the framework then I'd
be inclined to add a hidden field for each of your input fields to store the
previous content or a hash of the content if it were big, and use that on
post back to determine what had changed.

However you do it, you'll be the one maintaining it and working with it so
I'd suggest going for the solution which you are happiest working with.
 
M

Martin

Thank you, Brian. You answered my question.

BTW, as I noted in my original post, javascript is already being used
in the page (for other purposes). The user will, without question,
have js enabled.
 
J

jeff

Thank you, Brian. You answered my question.

/*

Or simply put a hidden field after each input.

<input type="text" name="my_field_1" ....><input type="hidden"
name="my_field_1_saved" ...>

Iterate through the fields and compare.

Could be done either server or client side.

*/
 

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,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top