Computing.Net > Forums > Web Development > Creating javascript arrays in forms

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Creating javascript arrays in forms

Reply to Message Icon

Name: rjm_810
Date: February 13, 2005 at 10:48:43 Pacific
OS: XP
CPU/Ram: pent 4
Comment:

Hi,

I have created a form using javascript, but now the problem is that the form should use the information inputted by the user on the form and generate a price. In order for this to happen I guess I have to create an Array/Table and be able to associate each selection with a price so when the user submits the form the Price would be generated, being the total sum of prices for each option selected...

Can anyone assist me with this I would really appreciate it.

Thanks in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: February 13, 2005 at 11:55:42 Pacific
Reply:

If you were to provide a link to the page you have so far I could give more detailed advice. But, here is what I can tell you for now:

Create an array within JS and just enter all the prices for the options in the same order that they exist in the select list. Example:

price1 = new Array (5.20,25.00,19.95)

You have multiple select lists. If each select list has different options, then you will need a different array for each. Just give them names that make sense to you.

You will need to create a field in which to store the total (for our examples let's say a text field with the name 'Total'). you can make this a hidden field if needed.

Then create a function to add all the prices and populate the total field:

function sumTotal() {
//Create shortcut to form element
var f = document.forms[0];
//Create the total variable
var totPrice = 0;
//Add the prices
totPrice += price1[o.select1.selectedIndex];
totPrice += price1[o.select2.selectedIndex];
totPrice += price1[o.select3.selectedIndex];
//Populate the total
o.Total.value = totPrice;
}

I would call this function onChange of the select lists and onSubmit of the form.

Michael J


0
Reply to Message Icon

Related Posts

See More


Flash Animation Size Meta Tags



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Creating javascript arrays in forms

Get Method with javascript array www.computing.net/answers/webdevel/get-method-with-javascript-array-/2362.html

creating an auto complete form www.computing.net/answers/webdevel/creating-an-auto-complete-form/3630.html

javascript within a form www.computing.net/answers/webdevel/javascript-within-a-form/1105.html