Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question assigning value to input

    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:

    Code:
    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...

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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.
    http://www.w3schools.com/js/js_switch.asp

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

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New Coder
    Join Date
    Jan 2007
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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;

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •