I have an ASP VBScript recordset which supplies the value and the label of a select box (ClientID and ClientName respectively).
I've created a JS array called "aTrusts" from this recordset so that I can use the data to populate text fields in my form with values, depending on the value of the select box.
So obviously I need to write a JS function, triggered by OnChange in the select box, which finds the relevant "row" and "column" of the array by looking for the ClientID and sets the value of the text field accordingly.
How the flying f*ck to I do this? lol
Is it even possible? Does JS support multi-dimensional arrays?
HELP!
pyro
07-18-2003, 08:29 AM
Not sure exactly what you are asking, but javascript does sorta support multidimentional arrays. They aren't true multidimentional arrays, but they work well enough.
<script type="text/javascript">
myarray = new Array();
myarray[0] = new Array("1","2","3");
myarray[1] = new Array("A","B","C");
document.write(myarray[0][1]);
document.write(myarray[1][2]);
</script>
idiotbear
07-18-2003, 08:35 AM
right
depending on the documents.forms[0].trust.value (which is a select box), I need to populate a text field with the corresponding address from the following array, which looks like this (this is only 2 "rows" of the real thing - there's actually 400 of them):
var aTrusts = new Array(
new Array('Deutsche International Trust Corporation (C.I.) Limited','St Paul's Gate','New Street','St Helier','Jersey','JE4 8ZB','','135 Bishopsgate Limited','R50418'),
new Array('Hampden House','Great Hampden','Buckinghamshire','HP16 9RD','','','','AA Mutual International Insurance Co Ltd Trust','R50595')
The value in documents.forms[0].trust is the LAST value in each "row" above, eg. R50418 or R50595.
So when documents.forms[0].trust is R50595, I want a form field called documents.forms[0].address1 to populate with "'Deutsche International Trust Corporation (C.I.) Limited". Geddit?
R
pyro
07-18-2003, 09:03 AM
First of all, you were setting up your multidimensional array wrong. You need to do it like I showed above... Anyway, give this code a try:
<html>
<head>
<script type="text/javascript">
var aTrusts = new Array();
aTrusts[0] = new Array('Deutsche International Trust Corporation (C.I.) Limited','St Paul\'s Gate','New Street','St Helier','Jersey','JE4 8ZB','','135 Bishopsgate Limited','R50418');
aTrusts[1] = new Array('Hampden House','Great Hampden','Buckinghamshire','HP16 9RD','','','','AA Mutual International Insurance Co Ltd Trust','R50595');
function setValues(sel) {
val = sel.options[sel.selectedIndex].value;
x = 0;
for (i=0; i<aTrusts.length; i++) {
if (aTrusts[i][8] == val) {
comp = aTrusts[i][0];
x = 1
}
}
if (x == 1) {
document.forms[0].address.value = comp;
}
else {
alert ("Value not found");
}
}
</script>
</head>
<body>
<form name="myform">
<select name="myselect" onchange="setValues(this);">
<option value="Please Choose">Please Choose</option>
<option value="R50418">R50418</option>
<option value="R50595">R50595</option>
<option value="R50596">R50596</option>
</select>
<br>
<input type="text" name="address" style="width: 250px;">
</form>
</body>
</html>
idiotbear
07-18-2003, 09:56 AM
the difficulty is trying to get it in that format - it's VBScript output originally, and I have to do a lot of very nasty string manipulation in VBS just to get it looking like the above.... it's very tricky to get it to display the iteration increments ([1], [2] etc)
pyro
07-18-2003, 10:05 AM
Well, I don't know VBScript, so I can't help you with that part, but the above is what you need to do in javascript...
idiotbear
07-18-2003, 10:07 AM
yep, gotcha. I'll get there ;)
idiotbear
07-21-2003, 04:16 AM
OK...
Through a superhuman effort in VBScript ;) I've now forced my VBS recordset into a nice JS array.
Here's a couple of example "rows":
var aTrusts = new Array()
aTrusts[0] = New Array(clientno1, client1address1, client1address2, client1address3, client1address4, client1address5, client1address6; );
I want my combo (document.forms[0].trust) to call a function onchange, so that if document.forms[0].trust.value = clientno1, it will set the values of document.forms[0].clientaddress to client1address1 from the array, etc etc
does that make sense?
R
idiotbear
07-21-2003, 05:12 AM
pyro - I'm getting an "object expected" error when I call your very nice function. any ideas?
R
pyro
07-21-2003, 07:16 AM
Can I see the code you are using?
idiotbear
07-21-2003, 08:27 AM
Sure. Here's the lot.
(please note, I have removed most of the array and option box values, as there are 403 rows of each, and they're not necessary to understand the page.)
<html><Head>
<Script language="JavaScript" type="text/javascript">
var aClients = new Array()
aClients[0] = new Array('recordID','addressLine1,'addressLine2','add
ressLine3','addressLine4','addressLine5','addressL
ine6','postcode');
aClients[1] = new Array ('recordID1','addressLine1,'addressLine2','address
Line3','addressLine4','addressLine5','addressLine6
','postcode');
function setValues(sel)
{
val = sel.options[sel.selectedIndex].value;
x = 0;
for (i=0; i<aClients.length; i++)
{
if (aClients[i][0] == val) {
comp = aClients[i][10];
x = 1
}
}
if (x == 1)
{
document.forms[0].InvAddress.value = comp;
}
else {
alert ("Value not found");
}
}
</script>
<link href="css/styles.css" rel="stylesheet" type="text/css">
<title>Add a New Fee</title>
<body class="main" >
<table width="750" border="0" cellspacing="0" cellpadding="4">
<tr>
<td width="192" height="65"><img src="images/MainLogoCorp.jpg" width="192" height="55"></td>
<td width="558" align="right" valign="bottom">add a new fee</td>
</tr>
<tr>
<td height="20" colspan="2"><img src="images/divider.gif" width="750" height="10"></td>
</tr>
<tr>
<td height="20" colspan="2"><p><font size="2">Please enter the details of the new fee below.
All fields are required.</font></p>
<p><font size="2">When you have finished, click "Add New". </font></p></td>
</tr>
</table>
<form action="regfee.asp" method="post" name="fees" id="fees" onSubmit="submitonce(this)">
<table width="750" border="1" cellpadding="2" cellspacing="4" bordercolor="#FFFFFF" class="main">
<tr>
<td width="18%" align="left" valign="middle">Client:</td>
<td colspan="3">
<select class="form" name="Client" id="Client" onChange="setValues(this);">
<option value="">Please Select</option>