Click to See Complete Forum and Search --> : Newbie help please.....
davidbook
02-27-2003, 01:31 PM
I am trying do do what I thought was simple...I have a middle ware app making a list of form fields. The field elements are text and contain numbers only. The numbers in the text fields (what the user enters) are used to detemine the "order of a list". So....If there are 5 form fields on a page, then the text fields would be numbered 1,2,3,4,5. The user has the option of "changing the order" of the products listed, so...I have figured out how to limit the entry to numbers only, along with not allowing the entry of a number larger than the number of text fields. (if there are only 5 fields, then the "max order number" can only be 5, so...
Here is my problem...when a user changes one of the text fields, we ONE other text field on the form will now contain a duplicate value, so we need to "swap" the values. In other words, no text fields can contain the same "order by number". I have tried to loop through the form elements to compare, then switch but it doesn't seem to work?? Here is my code....
formField.value is "passed in to the function"
var y = formField.value
var z = 0
for (x=0; x < document.forms[0].elements.length; x++){
//get the value of the element being inspected
z = document.forms[0].elements[x].value;
//if z is == y then switch the values
if(z >= y){
document.forms[<%=iWhatForm%>].elements[x].value = y;
formField.value = z;
Thanks in advance...
David
Nedals
02-27-2003, 02:25 PM
Interesting!!!
<html><head><title>Untitled</title>
<script type="text/javascript">
var numAry = new Array('','','','','',''); // used to track values prior to change for each cell
function update(num,thiscell) {
var imax = 5; // maximium value allowable
var anothercell = -1; // set to -1 since valid num between 0 and (imax-1)
if ((num<0) || (num>imax)) { alert(num + ' is out of range'); return; }
// loop to check if num already exists in ANOTHER cell and get the cell number
for (var i=0; i<imax; i++) {
if ((num == document.theform.elements[i].value) && (thiscell != i)) { anothercell = i }
}
if (anothercell > -1) { // another value found
numAry[anothercell] = document.theform.elements[thiscell].value;
document.theform.elements[anothercell].value = numAry[thiscell]; // update the array and the anothercell.value
}
numAry[thiscell] = num;
document.theform.elements[thiscell].value = num; // update array and this cell
}
</script>
</head>
<body bgcolor="#ffffff">
<form name="theform">
<input type="text" onChange="update(this.value,0)"><br>
<input type="text" onChange="update(this.value,1)"><br>
<input type="text" onChange="update(this.value,2)"><br>
<input type="text" onChange="update(this.value,3)"><br>
<input type="text" onChange="update(this.value,4)"><br>
</form>
</body></html>
Try this! :)
davidbook
02-27-2003, 02:52 PM
Look like it will work, (i'm laughing, like I would know), I'll get figured out then post back.
davidbook
02-27-2003, 03:47 PM
//here is what i have
<script Language = Javascript>
//this script is designed to find duplicate form field entries,
//then "swap" the values for the order by list
// <% %> contained in the javascript are server side to build
//dynamic array of elements to "plant" in our client side javascript, so....
function update(num,thiscell){
var numAry = new <%=arrThis%>; // used to track values
//prior to change for each cell , arrThis will
// look like Array('','','','') etc.
var imax = <%=iHowMany%>;
// maximium value allowable
var anothercell = -1;
// set to -1 since valid num between 0 and (imax-1)
if ((num<0) || (num>imax)) {
alert(num + ' is out of range');
return;}
// loop to check if num already exists in ANOTHER cell and
//get the cell number
for (var i=0; i<imax; i++){
if ((num == document.<%=iWhatForm%>.elements[i].value) && (thiscell != i)){
anothercell = i
}
}
if (anothercell > -1){ // another value found , there will always will be a matching number when a form field is changed
numAry[anothercell] = document.<%=iWhatForm%>.elements[thiscell].value;
document.<%=iWhatForm%>.elements[anothercell].value = numAry[thiscell]; // update the array and the anothercell.value
}
numAry[thiscell] = num;
document.<%=iWhatForm%>.elements[thiscell].value = num;
// update array and this cell
}
</script>
I am getting an "object Expeted" error in browser status bar...I looked at the source after running the script and all looks exactly like i expected. The <% %> inserted just what I wanted. ....Any ideas?
Nedals
02-27-2003, 04:30 PM
1. The numAry must be outside the function. If you are not predefining the numbers, it can be preset as shown.
2. I recomend you add (inside the function)
var thisform = document.<%=iWhatForm%>
and replace (4 places I think)
' document.<%=iWhatForm%>. ' with ' thisform. '
if (anothercell > -1){ // another value found , there will always will be a matching number when a form field is changed
Only if you set the value in each cell server side, otherwise they will be null. The values in the cells and the array MUST match at startup.
I'm not familiar with SSI so I can't help in that area.
davidbook
02-27-2003, 06:06 PM
It should work with your input. I already did the myForm = variable thing....thanks