Enjoy an ad free experience by logging in. Not a member yet? Register.


Results 1 to 8 of 8
Thread: Special price calculator

12222010, 12:51 PM #1
 Join Date
 Dec 2010
 Posts
 5
 Thanks
 0
 Thanked 0 Times in 0 Posts
Special price calculator
Hallo.
I am a total newbie to JavaScript coding, but I would like to have a price calculator on our internal website.
It should calculate the square meter of the product and then multiply it with the square meter price, multiply it with quantity and then plus it with the startup cost. But... if the square meter extend 20 m2 the square meter price should be lower, and also lower when it hit 50 m2. And at 100 m2 it should write that you have to contact sales department.
Can this be done with JavaScript?
Here is an example:
6 pieces of 2 x 4 meter.
2x4 = 8 m2
6x8 = 48 total m2
48 m2 x price2 x startup = total price
I really hope that someone can help me...
//Daniel
12222010, 01:38 PM
#2
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 17,730
 Thanks
 202
 Thanked 2,508 Times in 2,486 Posts
It can indeed be done with Javascript  but the forum is not a free coding service, especially for commercial applications, and in any case your examples are too vague and incomplete to be useful. My advice would be to learn JavaScript (and HTML), then if you have specific coding problems feel free to come back for guidance/hints/corrections.
I expect you realise that the biggest part of any code such as this is validation  that the values entered by the user are sensible, so if for example a number is expected a nonnumeric value is rejected.
It is your responsibility to die() if necessary…..  PHP Manual
12222010, 03:08 PM
#3
 Join Date
 Dec 2010
 Posts
 5
 Thanks
 0
 Thanked 0 Times in 0 Posts
Hi Philip.
Thank you for you answer and I fully understand you.
I all ready have a JavaScript calculator, but I would like to extend it.
I would like to have more "basisprice".
So I one price for 1  20 (quantity x high x wide = m2)
One price for 20  50
And one price for 50  99
Can this be done?
Check this script:
<script type="text/javascript">
function beregn() {
var basisprice = 70;
var high = document.calcutalor.high.value;
if (isNaN (high)  high < 0  high >10) {
alert ("Number must be between 1  10");
document.calcutalor.high.value = "";
document.calcutalor.high.focus();
return false;
}
var wide = document.calcutalor.wide.value;
if (isNaN (wide)  wide < 0  wide >10) {
alert ("Number must be between 1  10");
document.calcutalor.wide.value = "";
document.calcutalor.wide.focus();
return false;
}
var quantity = document.calcutalor.quantity.value;
if (isNaN (quantity)  quantity < 0  quantity >10) {
alert ("Number must be between 1  10");
document.calcutalor.quantity.value = "";
document.calcutalor.quantity.focus();
return false;
}
var total = "Total price: ";
totalprice = (quantity * high * wide * basisprice) + 50;
document.getElementById('result').innerHTML = totalprice;
}
</script>
12222010, 05:10 PM
#4
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 17,730
 Thanks
 202
 Thanked 2,508 Times in 2,486 Posts
That's more like it!
Note you cannot have a price for 120 and also for 2050 (must be 21 to 50).Code:var sqmtrs = high*wide; var basisprice1 = 70; // or whatever the price is var basisprice2 = 60; var basisprice3 = 55; if (sqmtrs > 99) {alert ("Contact Sales Department"); return false} if (sqmtrs<=20) {totalprice = (quantity * sqmtrs * basisprice1) + 50}; if (sqmtrs>20 && sqmtrs <=50) {totalprice = (quantity * sqmtrs * basisprice2) + 50}; if (sqmtrs>50) {totalprice = (quantity * sqmtrs * basisprice3) + 50}; document.getElementById('result').innerHTML = totalprice.toFixed(2); // 2 decimal places
I am unclear if the sizes must be integers or whether decimal values are allowed. You might prefer
var sqmtrs = Math.ceil(high*wide); // round up to nearest integer
To overcome a bug in Firefox, replace
document.formname.fieldname.focus();
by
setTimeout("document.formname.fieldname.focus()", 25);
Last edited by Philip M; 12222010 at 05:50 PM.
01032011, 08:46 PM
#5
 Join Date
 Dec 2010
 Posts
 5
 Thanks
 0
 Thanked 0 Times in 0 Posts
Hi Philip.
Thank you very much for your answer. I appreciate it.
But for some reason I cant get it to work. When I make a calculation it is all ways using basisprice1, no mather what I enter.
Do you know what I have done wrong??
Here is the full script:
<script type="text/javascript">
function calculate() {
var basisprice1 = 100;
var basisprice2 = 90;
var basisprice3 = 80;
var high = document.calculator.high.value;
if (isNaN (high)  high < 0  high >50) {
alert ("Enter 150");
document.calculator.high.value = "";
document.calculator.high.focus();
return false;
}
var width = document.calculator.width.value;
if (isNaN (width)  width < 0  width >50) {
alert ("Enter 150");
document.calculator.width.value = "";
document.calculator.width.focus();
return false;
}
var quantity = document.calculator.quantity.value;
if (isNaN (quantity)  quantity < 0  quantity >200) {
alert ("Enter 1200");
document.calculator.quantity.value = "";
document.calculator.quantity.focus();
return false;
}
var total = "Totalprice: ";
var piece = "Price per piece: ";
var kr = " DKK.";
var sqmtrs = high*width;
if (sqmtrs > 99) {alert ("Contact Sales Department"); return false};
if (sqmtrs<=20) {totalprice = (quantity * sqmtrs * basisprice1) + 50};
if (sqmtrs>20 && sqmtrs <=50) {totalprice = (quantity * sqmtrs * basisprice2) + 50};
if (sqmtrs>50) {totalprice = (quantity * sqmtrs * basisprice3) + 50};
document.getElementById('piece').innerHTML = piece;
document.getElementById('piecepris').innerHTML = totalprice / quantity;
document.getElementById('result').innerHTML = totalprice.toFixed(2);
document.getElementById('total').innerHTML = total;
document.getElementById('kr').innerHTML = kr;
document.getElementById('kr2').innerHTML = kr;
}
</script>
01042011, 12:40 AM
#6
Um...Philip, isn't this is *EXACTLY* the same homework problem you gave the same answer for a few months back??
poulsen: The code as given in your last post worked 100% correctly for me.
Except: The discount pricing is based *ONLY* on the height and width. The quantity is not used in calculating the discount. That's now what you asked for in your original post.
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
01042011, 11:09 AM
#7
 Join Date
 Dec 2010
 Posts
 5
 Thanks
 0
 Thanked 0 Times in 0 Posts
Off course...
Now it works, thank you...
Do you know how I can return the price pr. piece with two decimals?
document.getElementById('piecepris').innerHTML = totalprice / quantity;
01042011, 12:52 PM
#8
 Join Date
 Dec 2010
 Posts
 5
 Thanks
 0
 Thanked 0 Times in 0 Posts
I found the solution.
Thank you very much for your help