/    Sign up×
Community /Pin to ProfileBookmark

Validate dynamic fields in html form

I’ve set up an HTML form that has select boxes added to it dynamically, based on the number of records selected from a database.

A while loop in php adds the following to the form for each database record found:

[CODE]<tr>
<td>No. of Catalogues:&nbsp;
<select name=”catarray[]” onChange=”doTotal(this.form)”;>
<option value=”0″>0</option>
<option value=”4″>1</option>
<option value=”8″>2</option>
<option value=”12″>3</option>
<option value=”16″>4</option>
<option value=”20″>5</option>
</select>
</td>
</tr>[/CODE]

So if there are 3 records, the form displays:

No. of Catalogs: [I]select box[/I]
No. of Catalogs: [I]select box[/I]
No. of Catalogs: [I]select box[/I]

I want to use the javascript doTotal() function to total the values in all of the selected options as selections are made. I’m able to determine the number of select boxes that have been added to the form, but I can’t get the syntax right to access the select box values. I keep getting a ‘NaN’ or ‘undefined’ error.

Is this possible? – am I using the correct syntax for adding the ‘catarray’ select boxes? can anyone help with the javascript to access the value in each select box?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@VladdyDec 18.2004 — The value of the name attribute is not valid:

http://www.w3.org/TR/1999/REC-html401-19991224/types.html#type-cdata

Group all your select boxes that you want to add within a fieldset, give this fieldset an id and then get array of select elements:
myFieldset = document.getElementById('myFieldSetId');
selects = myFieldset.getElementsByTagName('select');


And get rid of the table - your select elements belong in a list if you must.
Copy linkTweet thisAlerts:
@NedalsDec 18.2004 — <select name="catarray[]" onChange="doTotal(this[this.selectedIndex].value)">
×

Success!

Help @andocam 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.24,
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,
)...