Click to See Complete Forum and Search --> : checking input data against an array to display specifc content in another element


supergreg
12-08-2003, 11:00 PM
I am in need of some help again. I have an order form that currently allows a person to order from a menu of items and the pricing of all selected items is calculated in real-time using some Javascript functions. However, I am stuck applying a standard delivery charge price of $5 to each order instead of basing the delivery charge price on location. Location would be identified by the end-user's zip code and cross-checked in an array of some sort that would have a handful of pre-entered zips along with associated price. When the user enters the zip, it would calculate the delivery charge price in real-time and add it to the total cost of the order.

Details:

My Order Function is below:
------------------------------
function meal() {
var f = document.form1;
// Quantity variables
var quantity1 = f.qty1.value;
var quantity2 = f.qty2.value;
var quantity3 = f.qty3.value;

// Price variables
var price1 = f.prc1.value;
var price2 = f.prc2.value;
var price3 = f.prc3.value;

// Subt-totals
f.t1.value = parseFloat(quantity1) * parseFloat(price1);
f.t2.value = parseFloat(quantity2) * parseFloat(price2);
f.t3.value = parseFloat(quantity3) * parseFloat(price3);

// Grand Total
f.delivery.value = 5; // This is the standard $5.00 delivery charge that I want to get rid of and replace with an array of zips and associated delivery prices.

f.quantity.value = parseFloat(f.qty1.value) + parseFloat(f.qty2.value) + parseFloat(f.qty3.value); // This is to calculate the total quantity of goods ordered

f.total.value = parseFloat(f.t1.value) + parseFloat(f.t2.value) + parseFloat(f.t3.value) + parseFloat(f.delivery.value); // This is to calculate the final price of the order
}


A sample of my Input items are below
------------------------------

<!-- The zip code entered by the end user -->
<input type="text" name="zip" class="contact">

<!-- The description of the meals -->
<input type="hidden" name="meal1" value="Plan C - Lunch and Dinner">

<!-- The quantity of the meals -->
<select name="qty3" class="quantity" onchange="meal()">
<option value="0">--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>

<!-- The price of the meals-->
<input type="hidden" name="prc3" value="80.00">

<!-- The total price of the meals -->
<input name="t3" type="text" class="total" onfocus="document.form1.t3.blur()" size="4" readonly="true">


A sample array that I would like to build:
------------------------------
20009 = $5
20010 = $10
20011 = $15
etc...

TheBearMay
12-09-2003, 08:09 AM
You could use a 2D array, maybe something akin to:

var twoD = [[20009, 20010, 20011 ][5, 10, 15]]

function findPrice(zipentered){
for(var i=0;i<twoD[1].length-1;i++){
if (twoD[i]==zipentered) return twoD[i][i]
}

...
f.delivery.value=findPrice(zip.value);