Click to See Complete Forum and Search --> : Javascript/CGI-Perl Problem


ltillner
04-17-2003, 11:29 AM
I have a CGI script that I've embedded a javascript in (to give me select boxes related to each other). I've been working on this for several days and have narrowed the problem down to the way the javascript works.

To see this script working go to:

http://avs-1.com/cgi-local/db3/db.cgi?db=demostruc&uid=demo.105059387870273&cap_add_form=1

The same script runs 3 times (it works with one exception)
First, it loads when you add a new record
then when you submit that record, the preview form
loads the script again so that the record can be edited if needed.
The third place the script loads is on the error page which allows you to fix your errors and resubmit the record, this doesn't cause a problem.

The problem is on the preview form.

In previewing the record, you see the data in text boxes as you submitted it

but it reloads the add form to allow you to make changes.
When this happens, the script tramples the values for the 2 select boxes with null values.

I need to get the select boxes to initialize with the submitted data (if selections were made on the add form).
OR
to save the data to a different variable and check it to see what value should be saved to the form.

THanks for any help! Below is the script code in question:


function fillSelectFromArray(selectCtrl, itemArray, goodPrompt, badPrompt, defaultItem) {
var i, j;
var prompt;
// empty existing items
for (i = selectCtrl.options.length; i >= 0; i--) {
selectCtrl.options[i] = null;
}
prompt = (itemArray != null) ? goodPrompt : badPrompt;
if (prompt == null) {
j = 0;
}
else {
selectCtrl.options[0] = new Option(prompt);
j = 1;
}
if (itemArray != null) {
// add new items
for (i = 0; i < itemArray.length; i++) {
selectCtrl.options[j] = new Option(itemArray[i][0]);
if (itemArray[i][1] != null) {
selectCtrl.options[j].value = itemArray[i][1];
}
j++;
}
// select first item (prompt) for sub list
selectCtrl.options[0].selected = true;
}
}
function init(frm) {
var combo = frm.Select_Box;
var combo2 = frm.Category;
frm.category.value = combo.options[combo.selectedIndex].value;
frm.occupancytype.value = combo2.options[combo2.selectedIndex].value;
}

DrDaMour
04-17-2003, 03:40 PM
selectlistobject.length = 0;

just removing the object doesn't change the length member of the select list automatically. i think instead of chaning all teh value to null in the first place, you shoudl just experiment with just chaning this member, as it may automatically delete the options indexed after length.

even if it doesn't it'd be faster to do this, then increase teh length every time you add to the select list

ltillner
04-18-2003, 10:33 AM
I'm sorry, I have no clue what you're talking about here. I don't know much about javascript. But how does this help me solve the issue of not overwriting existing values when the scrip loads in the preview record form?


Originally posted by DrDaMour
selectlistobject.length = 0;

just removing the object doesn't change the length member of the select list automatically. i think instead of chaning all teh value to null in the first place, you shoudl just experiment with just chaning this member, as it may automatically delete the options indexed after length.

even if it doesn't it'd be faster to do this, then increase teh length every time you add to the select list

DrDaMour
04-18-2003, 12:27 PM
the length member is the count of objects in a select list.

you need to set this any time you delete a option from the select list. You are just setting them to null, that's not removing them from the list.

so say you remove one option

blah.options[lastindex] = null;

well it will still be in the select list, you have to ALSO do
blah.length -= 1;

then it'll be gone