Hi,
I'm trying to do something similar. This 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 this is my select menu and total field (in php) -
<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>
The values seemed to get past through to the function okay so I'm lost as to why the fields aren't being updated...
8 36300
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!
acoder 16,027
Recognized Expert Moderator MVP
Merged post/threads + split first post from another thread.
acoder 16,027
Recognized Expert Moderator MVP
Don't use "total{$i}" . Use something like total or total2.
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?
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
-
-
<html>
-
<head>
-
<title>Random images same url</title>
-
</head>
-
-
<body>
-
-
<table>
-
<td>
-
<select id="qt" onChange="totalise()">
-
<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>
-
<select id="itemPrice" onChange="totalise()">
-
<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" type="text" readonly /></td>
-
</table>
-
-
</body>
-
-
<script type='text/javascript'>
-
-
function totalise() {
-
var qtd = document.getElementById('qt').value;
-
var price = document.getElementById('itemPrice').value;
-
var result = document.getElementById("total");
-
result.value = price * qtd;
-
}
-
</script>
-
-
</html>
-
That helps?
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
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.
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: 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>
|
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...
|
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....
|
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...
|
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:;"...
| |
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
|
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>
|
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...
|
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"...
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |