I had the following problem on an aspx page; I had to detect any changes made on a form but the form also included a dropdown box which was set for AutoPostBack in order to populated a Zip Code, City, etc.
The problem with the general checking of form elements and then confirming a change in records was that on post back the value returned became a default value on the form.
I had a hidden textbox hold the value 0 and 1, if a Zip Code was changed this textbox recieved a "1", otherwise it remained a "0". Since it was the only textbox on the form that would ever hold the value "1" I could use this to test for Zip Code post back.
The solution was:
Just thought I'd share it as well in case someone down the road ran into this. What I didn't like about it was the repetition of the "While" loop but I could not get a simple if...then to cooperate otherwise.
Note too that both confirms had to return true and false for form submittal. BillyRay provided in one of his threads the elegant use of returning stictly the confirm value in place of a true and false statement (I thought that was pretty slick).
The problem with the general checking of form elements and then confirming a change in records was that on post back the value returned became a default value on the form.
I had a hidden textbox hold the value 0 and 1, if a Zip Code was changed this textbox recieved a "1", otherwise it remained a "0". Since it was the only textbox on the form that would ever hold the value "1" I could use this to test for Zip Code post back.
The solution was:
Code:
<script language=javascript>
function checkF_status(myFrm) {
var el, opt, i = 0, j;
while (el = myFrm.elements[i++]) {
switch (el.type) {
case 'text' :
case 'textarea' :
if (el.value != el.defaultValue) return F_isChanged(myFrm);
break;
}
}
var j = 0;
while (el = myFrm.elements[j++]) {
switch (el.type) {
case 'text' :
case 'textarea' :
if (el.value == 1) return(confirm("You have changed your records, click OK to save...or Cancel to..."));
break;
}
}
var ans = confirm("No changes have been made to your current reocrd. Click OK to continue...or Cancel to...");
if (ans==true){
return true;
}
else{
return false;
}
}
function F_isChanged(myFrm){
return(confirm("You have changed your records, now they will be saved."));
}
</script>
Just thought I'd share it as well in case someone down the road ran into this. What I didn't like about it was the repetition of the "While" loop but I could not get a simple if...then to cooperate otherwise.
Note too that both confirms had to return true and false for form submittal. BillyRay provided in one of his threads the elegant use of returning stictly the confirm value in place of a true and false statement (I thought that was pretty slick).