User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 456,556 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,446 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 5488 | Replies: 21
Reply
Join Date: Oct 2007
Posts: 38
Reputation: kevindougans is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
kevindougans kevindougans is offline Offline
Light Poster

Question drop down quantity box to update price

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2007
Posts: 2,604
Reputation: MidiMagic is on a distinguished road 
Rep Power: 7
Solved Threads: 119
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Maven

Re: drop down quantity box to update price

  #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  
Join Date: Oct 2007
Posts: 38
Reputation: kevindougans is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
kevindougans kevindougans is offline Offline
Light Poster

Re: drop down quantity box to update price

  #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  
Join Date: Jan 2007
Posts: 2,604
Reputation: MidiMagic is on a distinguished road 
Rep Power: 7
Solved Threads: 119
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Maven

Re: drop down quantity box to update price

  #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:

var price, quantity;

price = document.forms.nameOfForm.total_price.value;
quantity = document.forms.nameOfForm.quan_ordered.value;
revenue = price * quantity;
document.forms.nameOfForm.part_revenue.value = revenue;
Daylight-saving time uses more gasoline
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 7,012
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 368
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: drop down quantity box to update price

  #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.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Jan 2007
Posts: 2,604
Reputation: MidiMagic is on a distinguished road 
Rep Power: 7
Solved Threads: 119
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Maven

Re: drop down quantity box to update price

  #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  
Join Date: Jun 2006
Location: India
Posts: 7,012
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 368
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: drop down quantity box to update price

  #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.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Jan 2007
Posts: 2,604
Reputation: MidiMagic is on a distinguished road 
Rep Power: 7
Solved Threads: 119
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Maven

Re: drop down quantity box to update price

  #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  
Join Date: Jun 2006
Location: India
Posts: 7,012
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 368
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: drop down quantity box to update price

  #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.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Jan 2007
Posts: 2,604
Reputation: MidiMagic is on a distinguished road 
Rep Power: 7
Solved Threads: 119
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Maven

Re: drop down quantity box to update price

  #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:
buton[i] = document.forms['namForm'].elements['buttonset[i]'].checked;
It should be:
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:
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;

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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 5:34 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC