If that doesn't work then can you either put the name value in quotes (e.g. name="people[0][name]") or use a different name (one without brackets)?
bol
Main Topics
Browse All TopicsI'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
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>
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
The first comment got it going by using '.elements[]'
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!
Business Accounts
Answer for Membership
by: b0lsc0ttPosted on 2007-08-02 at 18:41:06ID: 19622113
I don't see an array, or at least a Javascript array. Fields 3 and 4 have a name that uses a bracket but that doesn't make it a Javascript array. The brackets may cause problems for Javascript to access it but try ...
ents["peop le[0][coun try]"].val ue = countryCode ents["peop le[1][coun try]"].val ue = countryCode
document.forms.myForm.elem
document.forms.myForm.elem
Let me know how that works or if you get an error.
bol