/    Sign up×
Community /Pin to ProfileBookmark

Building Arrays

I have a form that has multiple fields on it (some required some optional), and multiple locations.

I decided to build arrays to hold the data. The problem I ran into was that if a field did not have a value, it does not appear to create a slot in the array.

So, if on a form Location 1 had “item 1”, Location 2 was blank, and Location 3 had “item 10” in a field. When I build the array it has “item 1;item 10”. So now “item 10” appears for Location 2 instead of Location 3.

To get around the problem, when a field was blank I created a dummy value of “$”, so the array then gets built as “item 1,$;item 10”. I then change the $ to a space when I redisplay the field.

What am I doing wrong?

Gary

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@pyroSep 23.2003 — We're going to need to see you code...
Copy linkTweet thisAlerts:
@gmorrisonauthorSep 24.2003 — function fnArray(varType, varLoc)

{

if(!document.forms[0].MainFrame)

{

main = parent.frames[0].document.forms[0];

} else {

main = document.forms[0];

}

/*

This is the main function call.

varType = "S" save fields; "D" delete location; "R" restore fields to form for display/update

varLoc = Location number

*/

var varArrayLen = 6
/*

varArrayLen = This is the size of the array

*/


var varType



/*

Since some of the fields have a default value, save the vaue of a required field. The save loop then checks to see if the

field is greater NULLS before doing the save, if NULLS nothing is saved

*
/



/*

The following array contains all the names of the array fields and the type of data. t = text; c= check box or radio button

It looks for a field called FIELDNAMES on the form which contains the type of data and the name of the fields on the form

*
/


var varFields = new Array();

var f2value = main.FieldNames1.value.replace(/ /g, "");
var f2value = f2value + ";" + main.FieldNames2.value.replace(/ /g, "");

var regexp = ';'
var varHold = f2value.split(regexp);



for (var i = 0; i < varHold.length; i++)
{

varFields[i] = new Array (varHold[i].substr(5), new Array (varHold[i].substr(1,1)));

}


/*

This routine loops thru the names of the fields in the varFields Array. It is assumed that the name of the array is the same as the field name prefixed with an 'm'

varLoc = Location parameter passed into the function

varArrayLen = Number of items in the saved array

varFields[i][0] = Name of data field

m + varFields[i][0] = Name of array field

varFields[i][1] = Type of data text or check box / radio button



*/







var NumFields = varFields.length - 1


for ( var i=0; i < NumFields; i++ )
{
if(varFields[i] > '')
{
if(varFields[i] > ' ')
{
if (varType == 'S')
{
fnPopulateArray(varLoc,varArrayLen,varFields[i] [0],'m'+varFields[i] [0], varFields [i][1]);
}

if(varType == 'D')
{
fnDeleteLoc(varLoc,varArrayLen,varFields[i] [0],'m'+varFields[i] [0]);
}

if(varType == 'R')
{
fnRestoreLoc(varLoc,varArrayLen,varFields[i] [0],'m'+varFields[i] [0], varFields [i][1]);
}
}
}

}

/*

Set focus to first entry field on the form, and initialize any fields with default values

*
/

// document.forms[0].Ord_SSN2.focus();

if (main.F1_Desc1.value == '')
{
main.F1_BC1[0].checked = 1;
main.F1_BC1[1].checked = 0;

main.F1_OC1[0].checked = 0;
main.F1_OC1[1].checked = 1;

main.F1_AI1[0].checked = 0;
main.F1_AI1[1].checked = 1;

main.F1_PC1[0].checked = 0;
main.F1_PC1[1].checked = 1;

main.F1_NumBldgs.value = "1";
main.SF3_BuildingGrade.value = "99";
main.SF3_BuildingAutoInc.value = "02";
main.SF3_BusiAutoInc.value = "02"

main.SF9_BP0437.value = "N";
main.SF9_BP0703.value = "N";
main.SF9_BP0484.value = "N";
main.SF9_BP0485.value = "N";

main.SF10_BP0447.value = "N";
main.SF10_BP04A49.value = "N";
main.SF10_BP04A48.value = "0";
main.SF10_BP04A50.value = "0";
main.SF10_BO00A11.value = "0";
main.SF10_BP04A02.value = "0";
main.SF10_BP04A16.value = "0";

main.SF11_BP0802.value = "N";
main.SF11_BP0805.value = "N";
main.SF11_BP0804.value = "N";
main.SF11_BP0801.value = "N";
main.SF11_BP08A01.value = "N";
main.SF11_BP0803.value = "N";
main.SF11_BP08A03.value = "N";
main.SF11_BP0012.value = "N";


}

}



function fnPopulateArray(varLoc,varArrayLen,varField,varCurrArray,varType)

{

if(!document.forms[0].MainFrame)

{

main = parent.frames[0].document.forms[0];

} else {

main = document.forms[0];

}

/*

This function is used to save the form field to the correct array location

*
/


if(varType == 'c')

{

var f1value = main[varField][0].checked

} else {

var f1value = main[varField].value

}


if(varType != 'c')

{

if(f1value == '')

{

f1value = '$'

}

}


if(varType != 'c')

{

if(f1value == ' ')

{

f1value = '$'

}

}


var f2value = main[varCurrArray].value;

var ArrayLen = f2value.length

if(ArrayLen >= varLoc)

{

var regexp = ';'

var varArray = f2value.split(regexp);

if(varType == 'c')

{

var z = main[varField].length

var f1value = '';

for (var y = 0; y < z; y++)

if (main[varField][y].checked)

{

f1value = f1value + 1


} else {

f1value = f1value + 0

}

}

varArray[varLoc]= f1value


main[varCurrArray].value = (varArray.join(";"));

main[varField].value = '';

}

}

function fnDeleteLoc(varLoc,varArrayLen,varField,varCurrArray)

{

if(!document.forms[0].MainFrame)

{

main = parent.frames[0].document.forms[0];

} else {

main = document.forms[0];

}

/*

This function will delete the location from the array, and then move any other values in the array up into the vacant area

The first location is then displayed on the form

*/

var f2value = main[varCurrArray].value;

var regexp = ';'

var varOldArray = f2value.split(regexp);

varOldArray[varLoc] = '';

var varNewArray = new Array(varArrayLen);

var x = 0;

for ( var i=0; i <= varArrayLen; i++ )

{

if (varOldArray[i] > '')

{

varNewArray[x] = varOldArray[i];

parseInt(x = x + 1);

}

}



main[varCurrArray].value = (varNewArray.join(";"));



}





function fnRestoreLoc(varLoc,varArrayLen,varField,varCurrArray,varType)

{

if(!document.forms[0].MainFrame)

{

main = parent.frames[0].document.forms[0];

} else {

main = document.forms[0];

}



/*

This routine will take the values in the array and place them into the form fields for display/update

*
/



var f2value = main[varCurrArray].value;


var regexp = ';'
var varArray = f2value.split(regexp);

var ArrayLen = varArray.length


if (varLoc < ArrayLen)

{

var CheckField = varArray[varLoc];

if (CheckField == '$')

{

CheckField = ''

}

if(varType == 't')

{

main[varField].value = CheckField;

} else {

var f1value = main[varCurrArray].value;

var z = main[varField].length

for (var y = 0; y < z; y++)

if(f1value.charAt(y) == '1')

{

main[varField][y].checked = 1

} else {

main[varField][y].checked = 0

}

}
}

}
×

Success!

Help @gmorrison spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...