drop down quantity box to update price

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Oct 2007
Posts: 38
Reputation: kevindougans is an unknown quantity at this point 
Solved Threads: 1
kevindougans kevindougans is offline Offline
Light Poster

drop down quantity box to update price

 
0
  #1
Oct 18th, 2007
Hello,

I hope someone has the time to help me.

I have a form that lists a variable price in
<input type=hidden name=price value=1>

I have a drop down box letting the user choose a quantity in
<select name=quantity>
<option value=1>1</option>
<option value=2>2</option>
etc...

I have a html form input field that I want to show the price in
<input type=text name=total_price>

How do I hook all this up so that the "total_price" field will display the variable price and multiply it by the quantity option to give a total numerical figure of cost?

Thanks,

KD
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 164
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: drop down quantity box to update price

 
0
  #2
Oct 19th, 2007
If the price is a price that changes daily (such as gasoline) and is stored on your server, JavaScript can't do the job. You need a server-side script.

If the price varies with the quantity ordered, then you need a script that reads the quantity, looks the price up in an array (indexed by the quantity ordered) and then display it in a text box.
Last edited by MidiMagic; Oct 19th, 2007 at 1:28 am.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 38
Reputation: kevindougans is an unknown quantity at this point 
Solved Threads: 1
kevindougans kevindougans is offline Offline
Light Poster

Re: drop down quantity box to update price

 
0
  #3
Oct 19th, 2007
I have the PHP doing its job. It's setting the
<input type=hidden name=price value=1>
or whatever the price may be.

I need to know what the javascript code is to make the box
<input type=text name=total_price>
to show the "price" * "quantity"

Anyone knock this up in javascript in 2 seconds for me?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 164
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: drop down quantity box to update price

 
0
  #4
Oct 21st, 2007
You need to put quote marks around the attributes "text" and "total_price".

You need to read the two values for price and quantity. Then you need to find the product. Finally, you must put the answer into the form:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var price, quantity;
  2.  
  3. price = document.forms.nameOfForm.total_price.value;
  4. quantity = document.forms.nameOfForm.quan_ordered.value;
  5. revenue = price * quantity;
  6. document.forms.nameOfForm.part_revenue.value = revenue;
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: drop down quantity box to update price

 
0
  #5
Oct 22nd, 2007
>price = document.forms.nameOfForm.total_price.value;
Incorrect way of accessing form elements. Each form object has an 'elements' host object which has all the form elements as it's properties. Correct way would be:

price = document.forms['nameOfForm'].elements['total_price'].value;
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 164
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: drop down quantity box to update price

 
0
  #6
Oct 25th, 2007
That syntax is necessary only if you are accessing the form using element numbers, variables containing the form or element names, or getElementByName.

None of my books on JavaScript show it for an element address not using variables. That is only one of many valid ways to address an element.

And it definitely does NOT work for addressing radio buttons, where you have to have an address of the syntax:

document.forms.myform.rabutton[i].checked

Is the W3C now deprecating JavaScript usage too?
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: drop down quantity box to update price

 
0
  #7
Oct 26th, 2007
> That syntax is necessary only if you are accessing the form using element numbers
Take a closer look at what I had posted. The fields or elements of a form should be accessed using the 'elements' nodelist or host object called HTMLCollection. Just because what you posted works now doesn't mean it always will.

> None of my books on JavaScript show it for an element address not using variables.
There are some things which you won't find in books. And anyways of the majority of books out there, only handful of them are worth reading.

> And it definitely does NOT work for addressing radio buttons
It certainly does. Please get your facts correct before putting a *NOT* there.

>Is the W3C now deprecating JavaScript usage too?
No, but you sure are using it the wrong way.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 164
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: drop down quantity box to update price

 
0
  #8
Oct 28th, 2007
How are we supposed to know this "correct" way of doing things if most of the sources have not published it? Clairvoyance?

What is your source for this requirement of using the 'elements' nodelist? All of my sources say that it is becoming the obsolete way of addressing elements.

Are we talking about the necessity of including the "elements" name in the description?

Or are we talking about different syntax? These are equivalent, unless you want the program to be able to change parts of the descriptor (which requires the third case):

document.forms.nameOfForm.part_revenue.value = revenue;

document.forms['nameOfForm'].elements['part_revenue'].value = revenue;

a = 'nameOfForm'; b = 'part_revenue'; document.forms[a].elements[b].value = revenue;

What is the "correct" syntax for using a radio button? Is it addressed as an array of arrays? I said it doesn't work, because I can't get it to work. If I try to address the element as a subscript, than I can't seem to address its own subscript.
Last edited by MidiMagic; Oct 28th, 2007 at 3:07 pm.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: drop down quantity box to update price

 
0
  #9
Oct 29th, 2007
> How are we supposed to know this "correct" way of doing things if most of the sources
> have not published it?
It's a real pity there are a lot of substandard tutorials / books out there to misguide beginners. Good sources are hard to find if one doesn't know what he is looking for.

> What is your source for this requirement of using the 'elements' nodelist?
DOM specification drafted by W3C.

> All of my sources say that it is becoming the obsolete way of addressing elements.
You are in bad company.

> What is the "correct" syntax for using a radio button?
Read this thoroughly for form access related queries. Then this.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 164
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: drop down quantity box to update price

 
0
  #10
Nov 2nd, 2007
OK, the pages you gave me even show the syntax I used as an alternative. I think you are interpreting them in a stricter sense than is required.

But they are also using deprecated forms in their pages, so they are not current. The name attribute is now deprecated in the form tag. The only place it works and validates is in the radio button. (I must use XHTML.)

And I see the difference between what I tried to use for radio buttons and what works. I was trying:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. buton[i] = document.forms['namForm'].elements['buttonset[i]'].checked;
It should be:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. buton[i] = document.forms['namForm'].elements['buttonset'].[i].checked;
But the following ARE completely interchangeable, according to those pages. One is not better than the other, unless dynamic addressing is needed:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.forms.nameOfForm.part_revenue.value = revenue;
  2.  
  3. document.forms['nameOfForm'].elements['part_revenue'].value = revenue;
  4.  
  5. a = 'nameOfForm'; b = 'part_revenue'; document.forms[a].elements[b].value = revenue;

The reason to use the first version is to keep the files short. Some ISPs (including mine) charge more for longer downloads. Keeping the files short allows more downloads per hour with the cheaper plan.
Last edited by MidiMagic; Nov 2nd, 2007 at 2:44 am.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC