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

Creating javascript arrays in forms

Reply to Message Icon

Original Message
Name: rjm_810
Date: February 13, 2005 at 10:48:43 Pacific
Subject: Creating javascript arrays in forms
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.


Report Offensive Message For Removal

Response Number 1
Name: Michael J (by mjdamato)
Date: February 13, 2005 at 11:55:42 Pacific
Subject: Creating javascript arrays in forms
Reply: (edit)

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


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Creating javascript arrays in forms

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software