473,396 Members | 2,009 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,396 software developers and data experts.

Java calculation on changeMe used in a for loop

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 #1
11 1453
r035198x
13,262 8TB
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!
Moved to PHP forum.
Mar 29 '07 #2
Shall I assume no-one can help me?
Apr 3 '07 #3
code green
1,726 Expert 1GB
I can't see a loop. The code you submitted was not php but HTML. This will not work in either[PHP]onChange=totalise($price,total{$i})>[/PHP]
Apr 3 '07 #4
Motoma
3,237 Expert 2GB
You will need to give each selection box a unique name and id, and refer to those in your Javascript onchange function.
Apr 3 '07 #5
okay I get that, but how do i concatenate the loop number to the name of the total field?

this is my loop

Expand|Select|Wrap|Line Numbers
  1.     for($i=0;$i<$num_rows;$i++){
  2.         $name = mysql_result($query,$i,2);
  3.         $price = mysql_result($query,$i,3);
  4.         $desc = mysql_result($query,$i,5);
  5.         $photo = mysql_result($query,$i,4);
  6.         echo("
  7.           <tr>
  8.             <td rowspan='3' valign='middle' align='center'><p><a href=\"javascript:popImage('../Images/$category/$photo','$name')\">
  9. <img src=\"../Images/$category/$photo\" border='0' width='50%' height='50%'></a></p>
  10.             <p class='style9'>Click to enlarge  </p></td>
  11.             <td colspan='3'>$desc</td>
  12.           </tr>
  13.           <tr>
  14.             <td>Unit Price</td>
  15.             <td>Quantity</td>
  16.             <td>Total</td>
  17.           </tr>
  18.           <tr>
  19.             <td>$$price</td>
  20.             <td>
  21.                 <select id='qty' onChange=totalise($price)>
  22.                     <option value='0'>0</option>
  23.                     <option value='1'>1</option>
  24.                     <option value='2'>2</option>
  25.                     <option value='3'>3</option>
  26.                     <option value='4'>4</option>
  27.                     <option value='5'>5</option>
  28.                     <option value='6'>6</option>
  29.                   </select>
  30.   </td>
  31.             <td>$  <input id='total{$i}' type='text' readonly size='4'/></td>
  32.           </tr>
  33.           <tr>
  34.             <td align='center'>$name</td>
  35.             <td>&nbsp;</td>
  36.             <td>&nbsp;</td>
  37.             <td><li>Add to Cart </li></td>
  38.           </tr>
  39.           ");
  40.       }
  41.  
What I am not sure about is how to add the loop number ( i ) to the input id=total
so it would loop like
total0
total1
total2
etc...

Thanks!
Apr 3 '07 #6
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 #7
Motoma
3,237 Expert 2GB
What are you getting as a result? Do you have any javascript errors?
Apr 4 '07 #8
I've changed my function to

Expand|Select|Wrap|Line Numbers
  1. function totalise(price,total,qty)
  2.     var qtd   = document.getElementById(qty).value;
  3.     var result = document.getElementById(total);
  4.     result.value = price * qtd;
  5. }
with the menu like this
Expand|Select|Wrap|Line Numbers
  1. <select id='qty{$i}' onChange=totalise($price,total{$i},qty{$i})>
I'm not getting any javascript errors, but nothing happens when I change the quantity value... I've checked the source code of the page and the naming of the looped values is working.. i.e. i get
qty0 total0
qty1 total1

I just can't figure out why nothing is returned to total field
Expand|Select|Wrap|Line Numbers
  1. <input id='total{$i}' type='text' readonly size='4'/>
Apr 4 '07 #9
I added a javascript alert in my function to see if I could debug, and when I change the menu I don't get the alert, which means the function isn't being called properly... Does anyone understand why???

Function:
Expand|Select|Wrap|Line Numbers
  1. <script type='text/javascript'>
  2. function totalise(price,total,qty)
  3.     alert(\"HELLOOOOO\"); 
  4.     var qtd   = document.getElementById(qty).value;
  5.     var result = document.getElementById(total);
  6.     result.value = price * qtd;
  7. }
  8. </script>
onChange:
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>
Apr 4 '07 #10
Motoma
3,237 Expert 2GB
No idea. Perhaps taking a look at the HTML output in a syntax-colored IDE would help you?
Apr 4 '07 #11
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 #12

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

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
1
by: PaullyB | last post by:
Hi, I am attempting to convert the following code written in c to equivalent java code. This is the CRC32 algorithm used by a GPS received I am interfacing with. Unfortunately, the CRC32 class...
2
by: DebbieG | last post by:
I have no idea how to start with this one. I have a subform where records could look similar to: Infraction Date Points 01/01/2000 3 06/01/2002 1 Somehow, I've got to...
17
by: Allerdyce.John | last post by:
Hi, I am trying to compare the amount of work between using STL algorithm VS a plain Java loop. Let's say the class Rect has 2 attributes: area, and areaPerCent. In Java, I just write a...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
17
by: Robert Baer | last post by:
I used Google and found some references for integer in Java. But "int" not only does not work, it also prevents reading X and Y coordinates of the mouse. What i would like to do: 1) Get X and Y...
0
by: Limpor | last post by:
Hello, I am new to learning java, and i am trying to build the class for a calculation card game, unfortunately i can't get the public Card top() and Card takeTop() method in the Stock class. Can...
5
by: ebrimagillen | last post by:
Hello mates, Just needed a solution on the exercises below. Exercise 2 A classic problem in introductory programming is the grains of rice on a chess board problem. Your program should...
318
by: King Raz | last post by:
The shootout site has benchmarks comparing different languages. It includes C# Mono vs Java but not C# .NET vs Java. So I went through all the benchmark on the site ... ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...

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.