473,378 Members | 1,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Javascript onChange calculate total quantity*price help!

Hi,
I'm trying to do something similar. This is my javascript

Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2. function totalise(price,total)
  3. {    
  4.     var qty   = window.document.getElementById('qty').value;
  5.     var result = total;
  6.     result.value = price * qty;
  7. }
  8. </script>
And this is my select menu and total field (in php)

Expand|Select|Wrap|Line Numbers
  1. <td>
  2.     <select id='qty' onChange=totalise($price,total{$i})>
  3.         <option value='0'>0</option>
  4.         <option value='1'>1</option>
  5.         <option value='2'>2</option>
  6.         <option value='3'>3</option>
  7.         <option value='4'>4</option>
  8.         <option value='5'>5</option>
  9.         <option value='6'>6</option>
  10.     </select>
  11.   </td>
  12. <td>$  <input id='total{$i}' type='text' readonly /></td>
The values seemed to get past through to the function okay so I'm lost as to why the fields aren't being updated...
Mar 29 '07 #1
8 36209
Hi everyone.
I'm trying to create a shopping cart (with PHP) and it's all going pretty good. Except I'm trying to make a quantity drop down menu which when you change the quantity the total field is updated... I had it working for one, but when I tried to implement it into a for loop (so that when there are more than one item on the page they work separately) nothing happens.

Here is my javascript
Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2.  
  3. function totalise(price,total)
  4. {    
  5.     var qty   = window.document.getElementById('qty').value;
  6.     var result = total;
  7.     result.value = price * qty;
  8. }
  9. </script>
  10.  
and here is a section of my PHP inside the for loop, $price comes from a database (and works) and $i is just the loop count number

Expand|Select|Wrap|Line Numbers
  1. <td>
  2.     <select id='qty' onChange=totalise($price,total{$i})>
  3.         <option value='0'>0</option>
  4.         <option value='1'>1</option>
  5.         <option value='2'>2</option>
  6.         <option value='3'>3</option>
  7.         <option value='4'>4</option>
  8.         <option value='5'>5</option>
  9.         <option value='6'>6</option>
  10.     </select>
  11.  </td>
  12. <td>$  <input id='total{$i}' type='text' readonly /></td>
  13.  
Any questions just ask! Thanks in advance!
Mar 29 '07 #2
acoder
16,027 Expert Mod 8TB
Merged post/threads + split first post from another thread.
Mar 29 '07 #3
acoder
16,027 Expert Mod 8TB
Don't use "total{$i}". Use something like total or total2.
Mar 29 '07 #4
Using total{i} takes the number of the loop and adds it to the total name
so when it runs it would be read as
total0
total1
total2

... which is what i should be using right?
Apr 3 '07 #5
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2.  
  3. <html>
  4. <head>
  5. <title>Random images same url</title>
  6. </head>
  7.  
  8. <body>
  9.  
  10. <table>
  11. <td>
  12.     <select id="qt" onChange="totalise()">
  13.         <option value="0">0</option>
  14.         <option value="1">1</option>
  15.         <option value="2">2</option>
  16.         <option value="3">3</option>
  17.         <option value="4">4</option>
  18.         <option value="5">5</option>
  19.         <option value="6">6</option>
  20.     </select>
  21. </td>
  22. <td>
  23.     <select id="itemPrice" onChange="totalise()">
  24.         <option value="0">0</option>
  25.         <option value="1">1</option>
  26.         <option value="2">2</option>
  27.         <option value="3">3</option>
  28.         <option value="4">4</option>
  29.         <option value="5">5</option>
  30.         <option value="6">6</option>
  31.     </select>
  32. <td>
  33. <td><input id="total" type="text" readonly /></td>
  34. </table>
  35.  
  36. </body>
  37.  
  38. <script type='text/javascript'>
  39.  
  40. function totalise() {    
  41.     var qtd   = document.getElementById('qt').value;
  42.     var price  = document.getElementById('itemPrice').value;
  43.     var result = document.getElementById("total");
  44.     result.value = price * qtd;
  45. }
  46. </script>
  47.  
  48. </html>
  49.  
That helps?
Apr 3 '07 #6
Ummm, no not really because my problem is that I want to implement this code into a loop, and I'm not sure how to...
Thanks anyway
Apr 3 '07 #7
I've figured out how to loop the id names I did it like this (where $i is the loop number), and looking at the source code when I view the page it is working fine

Expand|Select|Wrap|Line Numbers
  1. <select id='qty{$i}' onChange=totalise($price,total{$i},qty{$i})>
  2.                     <option value='0'>0</option>
  3.                     <option value='1'>1</option>
  4.                     <option value='2'>2</option>
  5.                     <option value='3'>3</option>
  6.                     <option value='4'>4</option>
  7.                     <option value='5'>5</option>
  8.                     <option value='6'>6</option>
  9.                   </select>
  10.   </td>
  11.             <td>$ <input id='total{$i}' type='text' readonly size='4'/></td>
  12.  
I thought by passing the values $total{$i} and qty{$i} to the javascript as variables so they may then call the proper id names (i.e. total0, qty0), then it would work...

This is my javascript code

Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2. function totalise(price,ttl,qt)
  3.     {    
  4.         var qty   = window.document.getElementById(qt).value;
  5.         var total   = window.document.getElementById(ttl).value;
  6.         var result = total;
  7.         result.value = price * qty;
  8.     }
To me it looks like it should work, can anyone point out something I've done wrong?
Thanks again.
Apr 3 '07 #8
Finally got it working, thanks to everyone who helped.
My last problem was passing total{$i} and qty{$i} through to the function, guess the javascript didnt like it...

For anyone who wants to know how I got it to work here is my working:
javascript:
Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2. function findTotal(price,i)
  3.     var qtd   = document.getElementById('qty'+i).value;
  4.     var result = document.getElementById('total'+i);
  5.     result.value = price * qtd;
  6. }
  7. </script>
And my working PHP/HTML (implemented into a for loop):
Expand|Select|Wrap|Line Numbers
  1. <select id='qty{$i}' onChange='findTotal($price,$i)'>
  2.     <option value='0'>0</option>
  3.     <option value='1'>1</option>
  4.     <option value='2'>2</option>
  5.     <option value='3'>3</option>
  6.     <option value='4'>4</option>
  7.     <option value='5'>5</option>
  8.     <option value='6'>6</option>
  9. </select>
  10.  
  11. $ <input id='total{$i}' type='text' readonly size='4'>
Apr 4 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Andy | last post by:
I'm not sure why my checkbox code is not working as per intended. It always keeps saying "Please select the department" even though I check the department... appreciate any help. <!DOCTYPE HTML...
4
by: Trip | last post by:
Please if someone can help me !!! I need client and server code(principle based on AJAX) for next problem: i have 3 <select> tags on html page.(it must be NO page reload(callback) only...
9
by: Mickey Segal | last post by:
The long-simmering Eolas patent dispute: http://www.microsoft.com/presspass/press/2003/oct03/10-06EOLASPR.mspx has led to an optional Microsoft Update: http://support.microsoft.com/kb/912945/en-us...
2
by: michaelmaud | last post by:
I am currently working on a photo gallery which loads in images on the fly. upon the page loading, it scans the directory in PHP and builds an array of the image names in a JavaScript Array format...
1
by: dreamlab | last post by:
Hello, Can one of you javascript wizards help out a newbie, please? I’ve got a formHandler that is supposed to check for a good email address and name in the form after clicking the submit...
1
by: marirs | last post by:
Hi, I have a selectbox in a JSP page. I have written a JS for changing certain data onchange of the box value. I also need to blackout some of the labels which come after the selectbox.I...
4
by: fredy | last post by:
I Need javascript help to retrive the cpu information of client in mozilla browser Note: <script language="javascript"> alert(navigator.cpuClass); </script>
3
by: gator6688 | last post by:
I have to write a program that asks for a cost-per-item, number of items purchased, and a discount rate. Then it should calculate the total cost, tax due, and amount due. I have to use the...
4
by: dupdupdup | last post by:
Hello there, im needed to develop a gallery for this website. im using hotspots to go to my frames in my flash Each hotspot goes to each frame. The flash is loaded properly. When i right click...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.