/    Sign up×
Community /Pin to ProfileBookmark

looping with variable names

I have an html page that has fields created with a loop in asp from a sql table and they are called Agency1, Agency2… and their are a variable number of them based on a hidden field called Total. When this form is submitted I need to put all of the data from these fields together in one field separated by a comma, so I created a loop as follows:

var agencynumber;
var numberOfAgents;

agent_code = Agency1.value;
numberOfAgents = Total.value;
numberOfAgents = numberOfAgents + 1
for (i = 2; i <= numberOfAgents; i++)

{agencynumber = “Agency” + i
agent_code = agent_code + “,” + agencynumber.value
}

what I want to happen is that agencynumber is a string called Agency1… Agency5 and that agencynumber.value gives me the value of the html form field agency, but that doesn’t seem to work. I’m pretty new to javascript so could use any help you could give me. Thanks, BostonRose

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@artoSep 01.2005 — This adds all fields named 'AgencyX', so no need for hidden field. [CODE]<html>
<head>
<script type="text/javascript">
function fnOnSubmit(form) {
var all='';

for (var i=0;i<form.elements.length;i++)
if (form.elements[i].name.indexOf('Agency')==0) {
if (all) all+=',';
all+=form.elements[i].value;
}

form.elements['All'].value=all;
}
</script>
</head>

<body>

<form onsubmit="fnOnSubmit(this)">
<input type="text" name="Agency1" value="value1"> <br>
<input type="text" name="Agency2" value="value2"> <br>
<input type="text" name="Agency3" value="value3"> <br>
<input type="text" name="Agency4" value="value4"> <br>
<input type="text" name="Agency5" value="value5"> <br>
<input type="text" name="All"> <br>
<input type="submit" value="send">
</form>

</body>
</html>[/CODE]
Arto
×

Success!

Help @Bostonrose 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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