Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with a function

Status
Not open for further replies.

milams

Programmer
May 28, 2004
32
US
Hi,

I'm a newbie when it comes to JavaScript. I'm trying to write a script where I have some hidden fields from a form. These fields are shipping prices. What I want to do is in the field item_quantity_1, when a new number is input in there I have an onchange command. That command triggers my function. The function seems to work alright, but I can't seem or don't know how to return the varibles into the hidden fields that I want to change befor I submit the form. for example, the regular shipping cost is $5.00 per unit. Someone inputs 4 into the item_quantity_1 field. What I want to change is the value of the shipping field base on the shipping cost multiplied by the item_quantity_1. How do I get it to change the value of the shipping field? That is where I get lost. here is the code below. Thanks.

<script language="JavaScript" type="text/javascript">
function CalculateShip(item_quantity_1, ship_method_price_3, ship_method_price_1, ship_method_price_2){
if(item_quantity_1 > 1){
var upsg = "14.95";
var upss = "40.00";
var upsn = "60.00";

var shipval_1 = item_quantity_1 * upsn;
var result_1 = shipval_1.toFixed(2);

var shipval_2 = item_quantity_1 * upss;
var result_2 = shipval_2.toFixed(2);

var shipval_3 = item_quantity_1 * upsg;
var result_3 = shipval_3.toFixed(2);
}
else{
var shipval_1 = ship_method_price_3;
}

}
</script>


HTML form below.

<form method="POST" action="" name="buynow">

<input type="hidden" name="item_name_1" value="REFWT-3125SE"/>
<input type="hidden" name="item_description_1" value="Refurbished WT-3125SE w/ Kybd, P/S"/>
<input type="text" name="item_quantity_1[/color red]" value="1" size="5" onchange="return CalculateShip(item_quantity_1.value, ship_method_price_3.value, ship_method_price_1.value, ship_method_price_2.value)"/>
<input type="hidden" name="item_price_1" value="179.99"/>

<input type="hidden" name="ship_method_name_1" value="UPS Next Day Air"/>
<input type="hidden" name="ship_method_price_1[/color red]" value="40.00"/>
<input type="hidden" name="ship_method_us_area_1" value="CONTINENTAL_48"/>

<input type="hidden" name="ship_method_name_2" value="UPS 2nd Day Air"/>
<input type="hidden" name="ship_method_price_2" value="29.90"/>
<input type="hidden" name="ship_method_us_area_2" value="FULL_50_STATES"/>

<input type="hidden" name="ship_method_name_3" value="UPS Ground"/>
<input type="text" name="ship_method_price_3" value="14.95" id="targetTD"/>
<input type="hidden" name="ship_method_us_area_3" value="FULL_50_STATES"/>

<input type="hidden" name="tax_rate" value="0.0825"/>
<input type="hidden" name="tax_us_state" value="TX"/>

<input type="hidden" name="_charset_"/>

</form>
 
you don't need the "" around you number values.
function CalculateShip(item_quantity_1, ship_method_price_3, ship_method_price_1, ship_method_price_2){
if(item_quantity_1 > 1){
var upsg = 14.95;
var upss = 40.00;
var upsn = 60.00;

var shipval_1 = item_quantity_1 * upsg;
var result_1 = shipval_1.toFixed(2);

var shipval_2 = item_quantity_1 * upss;
var result_2 = shipval_2.toFixed(2);

var shipval_3 = item_quantity_1 * upsg;
var result_3 = shipval_3.toFixed(2);

alert(item_quantity_1);
alert(upsn);
alert(shipval_1);
alert(result_1);

}else{
var shipval_1 = ship_method_price_3;
}
}
 
Or else convert your strings to numbers then back to string. if you need to use strings. Using parsefloat();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top