Jump to content

PhCyber Tunnel Available!

Get it on Playstore
Download Now!

Ad Placement For Rent

Ad Space For Rent. Get Your Product up here and let our users see them first when they visit our Forum. We have thousands of page views everyday!
More Info
Info
  • Welcome to PhCyber
  • Explore and Enjoy Browsing!
  • Lots of useful topics
  • Stay active and receive prizes
Sign in to follow this  
Cyber

Calculate Price Based on Quantity

Recommended Posts

 

First, we will create our items. For each item that we create in our table, we will add a quantity text box:
 

For Members Only. Register here https://phcyber.com/register


We name each field qnt_ID where ID starts from 1. When you create the items, make sure to use consecutive IDs as their names are used in the JavaScript function to sum all values. In our example, we created 4 items, each with a different price which is set in the data-price attribute for the corresponding quantity text field. Default values are set to 0 so no items are selected.

Then we created a JS function, CalculateItemsValue(), which checks the values for all quantity fields and sums them
 

 

For Members Only. Register here https://phcyber.com/register

 


We also define a variable for the total number of items that we have: 
 

For Members Only. Register here https://phcyber.com/register


Now what has left to be done is to add an onkeyup event to each of the quantity boxes. So when a key is pressed and released the CalculateItemsValue() JS function is executed, and the total price is calculated based on quantity selected for each of the items.

There is a div container with ID ItemsTotal where total value is printed using document.getElementById("ItemsTotal").innerHTML. 
 

For Members Only. Register here https://phcyber.com/register


You can check how this works here and download the demo page here.

HOW TO ADD A NEW ITEM
If you need to add new item to the table, follow these steps:

1) Add a new row to the table

 

For Members Only. Register here https://phcyber.com/register

It will be qnt_5 and price will be 99

2) Change JS variable total_items to 5

 

For Members Only. Register here https://phcyber.com/register

 

 

That's it you're done.

 


Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...