473,786 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript onChange calculate total quantity*price help!

21 New Member
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 36300
jimmyg123
21 New Member
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 Recognized Expert Moderator MVP
Merged post/threads + split first post from another thread.
Mar 29 '07 #3
acoder
16,027 Recognized Expert Moderator MVP
Don't use "total{$i}" . Use something like total or total2.
Mar 29 '07 #4
jimmyg123
21 New Member
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
Romulo NF
54 New Member
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
jimmyg123
21 New Member
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
jimmyg123
21 New Member
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
jimmyg123
21 New Member
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
1631
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 PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD><TITLE>Welcome to the Scripting Store</TITLE> <META http-equiv=Content-Type content="text/html; charset=windows-1252"> <SCRIPT>
4
1736
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 select(controles) regeneration !!!) In the first <select> goes countries, which must be pulled from some kind of database (whichever you want). after that if i select some country, second <select> must be filled with regions of that country, and when i...
9
2395
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 that creates non-JavaScript problems that can be fixed using JavaScript. With the Microsoft update installed, Java applets (as well as other content such as Flash videos) are unable to receive user input until an activating click or key press....
2
1904
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 i.e. Array("01.jpg', "02.jpg", ...) I am new to using Javascript so don't really know the terms for things. I have two navigation cursors which then cycle through the array. What I want is to be able to display the image number and the total...
1
2175
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 link and display a status error message above the form if either the E-mail and/or Name fields are blank, but instead the page does nothing. I think that the Submit link in the code below is probably the root of the problem: <a href="javascript:;"...
1
1250
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 tried directly disabling the labels but it doesnt work. Can anyone suggest a way out? Thanks, RAM
4
3816
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
11805
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 formulas: total cost = number of items * cost-per-item total cost (discounted) = total cost - (discount rate * total cost) tax due = total cost * TAXRATE amount due = total cost + tax due I am going to put what I have so far. Any suggestions would be...
4
2447
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 and click play it goes to the next frame. But the Hotspots just dont work with IE. For your information: This works with other browsers like firefox, safari and Opera. MY HEADER <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"...
0
9650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.