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!

Problem with javascript calculation method

Status
Not open for further replies.

garethtnash

Programmer
Jul 8, 2008
4
Hello All,

I'm trying to create a form that allows a user to calculate a total price based on quantity without refreshing, what I've got so far is a form witha textbox (txtQty) a hidden field (adprice) and a button (btnCalculate).

The adprice hidden field contains the base price of the product so for instance if someone buys one item they pay the base price (this will be poulated by a recordset)

Now what I'm trying to do is offer the following reductions -

QTY >=1 and <=1 adprice * 100%
QTY >=2 and <=10 adprice * 80%
QTY >=11 and <=20 adprice * 60%
QTY >=21 and <=50 adprice * 50%

and then based on the QTY entered in the txtQty textbox, display the total price in an innerhtml section, but also show a calculation of the new cost per unit

I've attached the code I've put together so far but it keeps falling down, and sadly I can't see what the problem is.. I would be really grateful for some help with this...

Thanks in advance
Attach Code

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>test</title>
</head>
<body class="threecol SubPage">
<div id="container">
<div id="main"><script type="text/javascript">
function OpenWin(url)
{
window.open(url,'win','scrollbars=1,status=0,resizable=0,width=200,height=265');
}


function btnCalculate_onclick()
{
var numQty;
var adprice;

if (isNaN(document.frmClient.txtQty.value))
{
alert('Please enter a number for advert quantity.');
}
else
{
numQty = parseInt(document.frmClient.txtQty.value);
adprice = parseInt(document.frmClient.adprice.value);

if((numQty >= 1) && (numQty <= 1)){document.getElementById('divPrice').innerHTML = '&pound;' + adprice + ' + VAT';
document.getElementById('maindivPrice').innerHTML = '&pound;' + Math.round(100*(adprice * numQty))/100 + ' + VAT';
}
else if((numQty >= 2) && (numQty <= 10)){document.getElementById('divPrice').innerHTML = '&pound;' + Math.round(adprice * numQty)/80)/numQty + ' + VAT';
document.getElementById('maindivPrice').innerHTML = '&pound;' + Math.round(adprice * numQty)/80 + ' + VAT';
}
else if((numQty >= 11) && (numQty <= 20)){document.getElementById('divPrice').innerHTML = '&pound;' + Math.round(adprice * numQty)/60)/numQty + ' + VAT';
document.getElementById('maindivPrice').innerHTML = '&pound;' + Math.round(adprice * numQty)/60 + ' + VAT';
}
else if((numQty >= 21) && (numQty <= 50)){document.getElementById('divPrice').innerHTML = '&pound;' + Math.round(adprice * numQty)/50)/numQty + ' + VAT';
document.getElementById('maindivPrice').innerHTML = '&pound;' + Math.round(adprice * numQty)/50 + ' + VAT';
}

else {
alert("Please contact us for more information.");
}
}
}
</script>

<div id="JBcontent" class="advertisers">
<ol><li class="advertisers_middle">
<h2>xxx</h2>
<ul>
<li>xxx</li>
</ul>
<div class="advertCalc">
<form name="frmClient" id="frmClient" method="post">
<label for="txtQty">Job Advertising Credits:</label> <input name="txtQty" id="txtQty" type="text" value="1">

<input type="button" name="btnCalculate" id="btnCalculate" value="Calculate" onclick="return btnCalculate_onclick();">
<input name="adprice" type="hidden" id="adprice" value="10" />
</form>
<div class="pricingDetails">
<span class="slidingCreditPrice">
Price Per Credit : <span name="divPrice" id="divPrice"><strong>&pound;99.00 + VAT</strong></span></span>

<br /><span class="totalPrice">Total Price : <span name="maindivPrice" id="maindivPrice"><strong>&pound;99.00 + VAT</strong></span></span> </div>
</div>
</li>
</ol>
</div>

</div>
</div>
</body>

</html>
 
Not enough open parentheses. Add an open parentheses before Math.round in the first statement of each of your else if statements:

Code:
if((numQty >= 1) && (numQty <= 1)){
    document.getElementById('divPrice').innerHTML = '&pound;' + adprice + ' + VAT';
    document.getElementById('maindivPrice').innerHTML = '&pound;' + Math.round(100*(adprice * numQty))/100 + ' + VAT';
} else if((numQty >= 2) && (numQty <= 10)){
    document.getElementById('divPrice').innerHTML = '&pound;' +   [red]([/red]Math.round(adprice * numQty)/80)/numQty + ' + VAT';
    document.getElementById('maindivPrice').innerHTML = '&pound;' + Math.round(adprice * numQty)/80 + ' + VAT';
} else if((numQty >= 11) && (numQty <= 20)){
    document.getElementById('divPrice').innerHTML = '&pound;' + [red]([/red]Math.round(adprice * numQty)/60)/numQty + ' + VAT';
    document.getElementById('maindivPrice').innerHTML = '&pound;' + Math.round(adprice * numQty)/60 + ' + VAT';
} else if((numQty >= 21) && (numQty <= 50)){
    document.getElementById('divPrice').innerHTML = '&pound;' + [red]([/red]Math.round(adprice * numQty)/50)/numQty + ' + VAT';
    document.getElementById('maindivPrice').innerHTML = '&pound;' + Math.round(adprice * numQty)/50 + ' + VAT';
} else {
    alert("Please contact us for more information.");
}

----
star.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top