speyfisher
asked on
Multidimensional arrays in Javascript
I've posted some code for a simple form. I have commented out the part of the function that isn't working as I would expect.
When a 'country' is selected from the drop-down menu, the corresponding 'country code' is populated in text fields of the form.
This works (form rows '1' and '2'):
document.forms.myForm.coun try0.value = countryCode
This does not work (form rows '3' and '4'):
document.forms.myForm.peop le[0][coun try].value = countryCode
I'd prefer to reference the form elements as in rows 3 and 4 but am uncertain how to reference multidimensional arrays in Javascript.
How should I go about this?
<html>
<head>
<title>People :: Names and Countries</title>
<script type="text/javascript">
<!--
var country = "";
function addCountry(countryCode) {
// This works -- 'country code' IS ADDED to rows 1 and 2
document.forms.myForm.coun try0.value = countryCode
document.forms.myForm.coun try1.value = countryCode
// This doesn't work -- 'country code' IS NOT ADDED to rows 3 and 4
// document.forms.myForm.peop le[0][coun try].value = countryCode
// document.forms.myForm.peop le[1][coun try].value = countryCode
}
//-->
</script>
</head>
<body>
<form name="myForm" method="post" action="processForm.php">
COUNTRY:
<select name="countryCode" onchange="addCountry(this. value);">
<option value="" selected>(Select COUNTRY)</option>
<option value="CAN">Canada</option >
<option value="FRA">France</option >
<option value="UK">United Kingdom</option>
</select>
<p>
1. NAME:<input type=text name=name0> COUNTRY:<input type=text name=country0><br />
2. NAME:<input type=text name=name1> COUNTRY:<input type=text name=country1><br />
3. NAME:<input type=text name=people[0][name]> COUNTRY:<input type=text name=people[0][country]><b r />
4. NAME:<input type=text name=people[1][name]> COUNTRY:<input type=text name=people[1][country]><b r />
</p>
</form>
</body>
</html>
When a 'country' is selected from the drop-down menu, the corresponding 'country code' is populated in text fields of the form.
This works (form rows '1' and '2'):
document.forms.myForm.coun
This does not work (form rows '3' and '4'):
document.forms.myForm.peop
I'd prefer to reference the form elements as in rows 3 and 4 but am uncertain how to reference multidimensional arrays in Javascript.
How should I go about this?
<html>
<head>
<title>People :: Names and Countries</title>
<script type="text/javascript">
<!--
var country = "";
function addCountry(countryCode) {
// This works -- 'country code' IS ADDED to rows 1 and 2
document.forms.myForm.coun
document.forms.myForm.coun
// This doesn't work -- 'country code' IS NOT ADDED to rows 3 and 4
// document.forms.myForm.peop
// document.forms.myForm.peop
}
//-->
</script>
</head>
<body>
<form name="myForm" method="post" action="processForm.php">
COUNTRY:
<select name="countryCode" onchange="addCountry(this.
<option value="" selected>(Select COUNTRY)</option>
<option value="CAN">Canada</option
<option value="FRA">France</option
<option value="UK">United Kingdom</option>
</select>
<p>
1. NAME:<input type=text name=name0> COUNTRY:<input type=text name=country0><br />
2. NAME:<input type=text name=name1> COUNTRY:<input type=text name=country1><br />
3. NAME:<input type=text name=people[0][name]> COUNTRY:<input type=text name=people[0][country]><b
4. NAME:<input type=text name=people[1][name]> COUNTRY:<input type=text name=people[1][country]><b
</p>
</form>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The first comment got it going by using '.elements[]'
i.e.
document.forms.myForm.elem ents["peop le[0][coun try]"].val ue = countryCode
The second comment-- (var name="...) value in quotes didn't do it. Using a different name without brackets worked fine in the example but I'd prefer the POST data sent to the php script be accessible something like: $country = $_POST['people'][$i]['coun try'];
Thanks bol!
i.e.
document.forms.myForm.elem
The second comment-- (var name="...) value in quotes didn't do it. Using a different name without brackets worked fine in the example but I'd prefer the POST data sent to the php script be accessible something like: $country = $_POST['people'][$i]['coun
Thanks bol!
Your welcome! I'm glad the first worked then. I have had issues before but I was pretty confident elements[] would be a way to do it.
Thanks for the grade, the points and the fun question.
bol
Thanks for the grade, the points and the fun question.
bol
bol