/    Sign up×
Community /Pin to ProfileBookmark

Volume based form calculation

I am having some limited success with a shopping cart display I am designing.

The aim is to enter your quantity in the first field (which will only accept integers) and as the number grows the price per item changes (volume based pricing) and the total is claculated. The prices are supplied by php and looped for each product. The price to volume information will be different for each product. There are 4 price bands with a lower and upper value.

I am getting the price calculated for the first price band but it never seems to progress to the next price band.

I am relatively new to javascript and this has been my most ambitios asking of it. Im I heading in the right direction? Is there anything I am missing?

The code is below:

[code]
<script language=”JavaScript”>
function calculate(total, price, quantity, price_1, price_2, price_3, price_4, lower_1, upper_1, lower_2, upper_2, lower_3, upper_3, lower_4, upper_4)
{
var re = new RegExp(/^d+$/); // only allow +ve integers
var m = re.exec(quantity.value);
if (m == null) { // reset fields if not integer
quantity.value = “”;
total.value = “”;
} else {
// the pricing bands
if(quantity.value >= lower_1.value && quantity.value <= upper_1.value){
price.value = price_1.value;
}else if(quantity.value >= lower_2.value && quantity.value <= upper_2.value){
price.value = price_2.value;
}else if(quantity.value >= lower_3.value && quantity.value <= upper_3.value){
price.value = price_3.value;
}else if(quantity.value >= lower_4.value && quantity.value <= upper_4.value){
price.value = price_4.value;
}
total.value = (price.value)*(quantity.value); // the calc
}
document.frmCart.total.value = price.value;
}

</script>
<form name=”frmCart” method=”get”>
<?php
$num = 0;
while($row = mysql_fetch_array($result)) // loop for each product
{
$num++; // Increment for each product
?>
<input type=”text” name=”quantity_<?=$num?>” size=”5″ onKeyUp=”calculate(document.frmCart.total_<?=$num?>, document.frmCart.price_<?=$num?>, document.frmCart.quantity_<?=$num?>, document.frmCart.price_1_<?=$num?>, document.frmCart.price_2_<?=$num?>, document.frmCart.price_3_<?=$num?>, document.frmCart.price_4_<?=$num?>, document.frmCart.quant_1_lower_<?=$num?>, document.frmCart.quant_1_upper_<?=$num?>, document.frmCart.quant_2_lower_<?=$num?>, document.frmCart.quant_2_upper_<?=$num?>, document.frmCart.quant_3_lower_<?=$num?>, document.frmCart.quant_3_upper_<?=$num?>, document.frmCart.quant_4_lower_<?=$num?>, document.frmCart.quant_4_upper_<?=$num?>)”>
<input class=”quantities” type=”hidden” name=”price_1_1″ value=”10.00″ size=”5″ >
<input class=”quantities” type=”hidden” name=”price_2_1″ value=”20.00″ size=”5″ >
<input class=”quantities” type=”hidden” name=”price_3_1″ value=”30.00″ size=”5″ >
<input class=”quantities” type=”hidden” name=”price_4_1″ value=”50.00″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_1_lower_1″ value=”1″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_1_upper_1″ value=”50″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_2_lower_1″ value=”51″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_2_upper_1″ value=”1000″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_3_lower_1″ value=”1001″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_3_upper_1″ value=”5000″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_4_lower_1″ value=”5001″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_4_upper_1″ value=”10000″ size=”5″ >
$<input class=”quantities” readonly type=”text” name=”price_1″ size=”5″ >
$<input class=”quantities” readonly type=”text” name=”total_<?=$num?>” size=”5″ >

</form>
<?php
}
?>[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@heals1icauthorJun 30.2005 — What is your point Willy?

It is not cross posting if the question exists in other web forums.

If you have nothing constructive to add, don't post!
Copy linkTweet thisAlerts:
@Willy_DuittJun 30.2005 — What is your point Willy?

It is not cross posting if the question exists in other web forums.

If you have nothing constructive to add, don't post![/QUOTE]


What's your point...

I'll post anything I want...

Would you like a nickle to buy a clue also??

I'm tired of explaining why I post links to cross-posts...

Try using the search feature if you want a clue for free...

Have many nice days;

.....Willy :p
Copy linkTweet thisAlerts:
@heals1icauthorJul 01.2005 — Is this constructive to getting assistance??
Copy linkTweet thisAlerts:
@Willy_DuittJul 01.2005 — Is this constructive to getting assistance??[/QUOTE]

You tell me...

You're the one making issue of my posting links to the numerous threads you have started on this very topic... Don't you think the other members here should have the ability to check if your question has already been answered elsewhere before they waste their time responding to an already answered question??

And why would you think you can tell me to respond or not...

If you do not like my posts... Don't read them...

If this thread continues to move off topic it is your fault not mine... ?

.....Willy
Copy linkTweet thisAlerts:
@heals1icauthorJul 01.2005 — You are possibly right, but;

Any feedback on the topic of the question is appreciated irrespective of double ups from different web forums. This provides the opportunity to have many unique perspectives on the solutions from different types of coders. As you are aware there are usually many solutions to a problem. The more individual solutions available, the more informed choice for the coder as to their most appropriate solution.

It is up to the individual to post an answer to the thread. I'm not ungrateful for any assistance from forums but I saw your links without an explination as not particularly helpful.

Anyway enough said from me, I have a coding issue to solve.
×

Success!

Help @heals1ic spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...