View Full Version : assigning value to input

Mar 15th, 2007, 03:02 AM
Hi, I'm trying to use javascript to

1. get the values of several inputs in a form

2. take those values (all strings) and add them to the value of another input (which already has it's own initial string value) on pressing the submit button;

here is what I have so far, which of course, doesn't work:

function CalculateOrder(form) {
var first = document.form.first_name.value;
var last = document.form.last_name.value;
var email = document.form.email.value;
var phone = document.form.night_phone_a.value + document.form.night_phone_b.value + document.form.night_phone_c.value;
var address = document.form.address1.value;
var cat = document.form.os1.value;

if (form.os0.value == "1/4 Page-6 months") form.amount.value = 155.00;
if (form.os0.value == "1/4 Page-1 year") form.amount.value = 285.00;
if (form.os0.value == "Business Card- 6 months") form.amount.value = 85.00;
if (form.os0.value == "Business Card- 1 year") form.amount.value = 150.00;
if (form.os0.value == "Classified- 3 lines") form.amount.value = 49.50;
if (form.os0.value == "Classified- 4 lines") form.amount.value = 67.50;
if (form.os0.value == "Classified- 5 lines") form.amount.value = 85.50;
if (form.os0.value == "Classified- 6 lines") form.amount.value = 103.50;
if (form.os0.value == "Classified- 7 lines") form.amount.value = 120.00;
if (form.os0.value == "Classified- 8 lines") form.amount.value = 136.50;
if (form.os0.value == "Classified- 9 lines") form.amount.value = 153.00;
if (form.os0.value == "Classified- 10 lines") form.amount.value = 169.50;
document.form.os1.value = cat + first + last + email + phone + address;

I had this script working with all the if statements. Those just assign a monetary value to one input based on a drop down menu in the form, and they worked. Now I am trying to add this new part with getting all the input values. I propbably wrote something completely wrong, but I couldn't google a solution...

Mar 15th, 2007, 09:23 PM
A few things I notice...

* I sure hope this is not for a real production ordering system. I love free stuff and all, but it's just a bad idea. Keep your logic, and prices, on the server unless you want people getting cool discounts.

* Strings should be quoted for best results.
if (form.os0.value == "1/4 Page-6 months") form.amount.value = "155.00";

* Use else or a switch when the thing you are checking is always the same. It is better (cleaner, easier to maintain) logic and it saves processing time on the client as the number of comparisons is fewer.
if (form.os0.value == "1/4 Page-6 months") form.amount.value = "155.00";
else if (form.os0.value == "1/4 Page-1 year") form.amount.value = "285.00";
else if (form.os0.value == "Business Card- 6 months") form.amount.value = "85.00";
and so on
I don't recall offhand if javascript allows a switch on text, but it's better than a bunch of else if-s if you can.

* javascript, being loosely typed, sometimes needs help when you use the + sign. If you mean to use it to add numbers, and those things are text, explicitly convert using Number() or ParseInt(). If you mean them to be text, sometimes you need to remind it with an empty string.
document.form.os1.value = '' + cat + first + last + email + phone + address;
Otherwise you can see interesting things when it decides to treat something as text when you wanted a number, or vice versa.

Mar 16th, 2007, 07:46 PM
no not a real production ordering system, just a paypal linked form. if they were to try and pay $0, then they would pay $0 and the business would not give them an ad space. there's no way to damage the bsuiness, unless they get a service charge for every order (I'll have to ask about that).

I will try your suggestions and see if I can't get that one input to get all those other variables added to it. Never heard the open quote thing, crossing my fingers

Mar 16th, 2007, 10:18 PM
Oh, one more thing.
IE might allow this, but it's not standard.

function CalculateOrder(form) {
var first = document.form.first_name.value;

Change it to this. "form" already has a meaning. It's best not to inadvertently override that meaning by calling a variable "form".

function CalculateOrder(frm) {
var first = frm.first_name.value;