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 -
<script type='text/javascript'>
-
-
function totalise(price,total)
-
{
-
var qty = window.document.getElementById('qty').value;
-
var result = total;
-
result.value = price * qty;
-
}
-
</script>
-
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 -
<td>
-
<select id='qty' onChange=totalise($price,total{$i})>
-
<option value='0'>0</option>
-
<option value='1'>1</option>
-
<option value='2'>2</option>
-
<option value='3'>3</option>
-
<option value='4'>4</option>
-
<option value='5'>5</option>
-
<option value='6'>6</option>
-
</select>
-
</td>
-
<td>$ <input id='total{$i}' type='text' readonly /></td>
-
Any questions just ask! Thanks in advance!
11 1453
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 -
<script type='text/javascript'>
-
-
function totalise(price,total)
-
{
-
var qty = window.document.getElementById('qty').value;
-
var result = total;
-
result.value = price * qty;
-
}
-
</script>
-
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 -
<td>
-
<select id='qty' onChange=totalise($price,total{$i})>
-
<option value='0'>0</option>
-
<option value='1'>1</option>
-
<option value='2'>2</option>
-
<option value='3'>3</option>
-
<option value='4'>4</option>
-
<option value='5'>5</option>
-
<option value='6'>6</option>
-
</select>
-
</td>
-
<td>$ <input id='total{$i}' type='text' readonly /></td>
-
Any questions just ask! Thanks in advance!
Moved to PHP forum.
Shall I assume no-one can help me?
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]
You will need to give each selection box a unique name and id, and refer to those in your Javascript onchange function.
okay I get that, but how do i concatenate the loop number to the name of the total field?
this is my loop -
for($i=0;$i<$num_rows;$i++){
-
$name = mysql_result($query,$i,2);
-
$price = mysql_result($query,$i,3);
-
$desc = mysql_result($query,$i,5);
-
$photo = mysql_result($query,$i,4);
-
echo("
-
<tr>
-
<td rowspan='3' valign='middle' align='center'><p><a href=\"javascript:popImage('../Images/$category/$photo','$name')\">
-
<img src=\"../Images/$category/$photo\" border='0' width='50%' height='50%'></a></p>
-
<p class='style9'>Click to enlarge </p></td>
-
<td colspan='3'>$desc</td>
-
</tr>
-
<tr>
-
<td>Unit Price</td>
-
<td>Quantity</td>
-
<td>Total</td>
-
</tr>
-
<tr>
-
<td>$$price</td>
-
<td>
-
<select id='qty' onChange=totalise($price)>
-
<option value='0'>0</option>
-
<option value='1'>1</option>
-
<option value='2'>2</option>
-
<option value='3'>3</option>
-
<option value='4'>4</option>
-
<option value='5'>5</option>
-
<option value='6'>6</option>
-
</select>
-
</td>
-
<td>$ <input id='total{$i}' type='text' readonly size='4'/></td>
-
</tr>
-
<tr>
-
<td align='center'>$name</td>
-
<td> </td>
-
<td> </td>
-
<td><li>Add to Cart </li></td>
-
</tr>
-
");
-
}
-
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!
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 -
<select id='qty{$i}' onChange=totalise($price,total{$i},qty{$i})>
-
<option value='0'>0</option>
-
<option value='1'>1</option>
-
<option value='2'>2</option>
-
<option value='3'>3</option>
-
<option value='4'>4</option>
-
<option value='5'>5</option>
-
<option value='6'>6</option>
-
</select>
-
</td>
-
<td>$ <input id='total{$i}' type='text' readonly size='4'/></td>
-
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 - <script type='text/javascript'>
-
function totalise(price,ttl,qt)
-
{
-
var qty = window.document.getElementById(qt).value;
-
var total = window.document.getElementById(ttl).value;
-
var result = total;
-
result.value = price * qty;
-
}
To me it looks like it should work, can anyone point out something I've done wrong?
Thanks again.
What are you getting as a result? Do you have any javascript errors?
I've changed my function to - function totalise(price,total,qty)
-
{
-
var qtd = document.getElementById(qty).value;
-
var result = document.getElementById(total);
-
result.value = price * qtd;
-
}
with the menu like this - <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 - <input id='total{$i}' type='text' readonly size='4'/>
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: - <script type='text/javascript'>
-
function totalise(price,total,qty)
-
{
-
alert(\"HELLOOOOO\");
-
var qtd = document.getElementById(qty).value;
-
var result = document.getElementById(total);
-
result.value = price * qtd;
-
}
-
</script>
onChange: - <select id='qty{$i}' onChange='totalise($price,total{$i},qty{$i})'>
-
<option value='0'>0</option>
-
<option value='1'>1</option>
-
<option value='2'>2</option>
-
<option value='3'>3</option>
-
<option value='4'>4</option>
-
<option value='5'>5</option>
-
<option value='6'>6</option>
-
</select>
No idea. Perhaps taking a look at the HTML output in a syntax-colored IDE would help you?
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 : -
<script type='text/javascript'>
-
function findTotal(price,i)
-
{
-
var qtd = document.getElementById('qty'+i).value;
-
var result = document.getElementById('total'+i);
-
result.value = price * qtd;
-
}
-
</script>
And my working PHP/HTML (implemented into a for loop): -
<select id='qty{$i}' onChange='findTotal($price,$i)'>
-
<option value='0'>0</option>
-
<option value='1'>1</option>
-
<option value='2'>2</option>
-
<option value='3'>3</option>
-
<option value='4'>4</option>
-
<option value='5'>5</option>
-
<option value='6'>6</option>
-
</select>
-
-
$ <input id='total{$i}' type='text' readonly size='4'>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 ...
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |