Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: n00b needs help

  1. #1
    Join Date
    Feb 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default n00b needs help

    I'm new to Javascript and can make sense of some - but I have a task which I cannot figure out how to do. Just learning - and enjoying it.

    If anyone can impart some wisdom - I'd appreciate it.

    I have a form which only validates the fields for being poulated.
    What I WANT to do is to populate a hidden field, based on a drop-down field.

    So - you select a category (let's say 1- 4).
    The Value of this field is passed to an Email handler.
    I want to change the Mailto value - based on the category value.

    How do I do it (at least conceptually).???

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    I'm not really sure what you mean by "Mailto" value, but try this:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <script type="text/javascript">
    function changeVal(elem) {
    document.getElementById("data").value = elem.options[elem.selectedIndex].value
    }
    </script>
    </head>
    <body>
    <select onchange="changeVal(this)">
    	<option value="value 1">My Option 1</option>
    	<option value="value 2">My Option 2</option>
    	<option value="value 3">My Option 3</option>
    	<option value="value 4">My Option 4</option>
    </select>
    <br><input id="data" type="hidden">
    </body>
    </html>
    That gets your values for corresponding selected options.
    Remove the type="hidden" part if you want to see the result.
    - Mike

  3. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Or maybe something more similar to:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <script type="text/javascript">
    var sent = "Send mail to: "
    function changeVal(elem) {
    var mailto = document.getElementById("mailer")
    mailto.href = "mailto:"+elem.options[elem.selectedIndex].value
    var txt = document.createTextNode(sent+elem.options[elem.selectedIndex].firstChild.nodeValue)
    while (mailto.firstChild) {
    	mailto.removeChild(mailto.firstChild)
    	}
    mailto.appendChild(txt)
    }
    </script>
    </head>
    <body>
    <select onchange="changeVal(this)">
    	<option value="mymail@mail.com">My E-mail</option>
    	<option value="value2">My Option 2</option>
    	<option value="value3">My Option 3</option>
    	<option value="value4">My Option 4</option>
    </select>
    <br><a href="#" id="mailer"></a>
    </body>
    </html>
    I'm still not sure what you're asking.
    - Mike

  4. #4
    Join Date
    Feb 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Mike.

    The form passes to a CDONTS object.
    The mailto is just a hidden field within the form:


    <input type="hidden" name="mailto" value="me@mysite.com>

    If someone selects category 1 - I want to send it to adam@mysite.com
    If someone selects category 2 - I want to send it to marcus@mysite.com

    Does that explain it?

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <script type="text/javascript">
      var addresses = [
        "adam@mysite.com",
        "marcus@mysite.com"
      ];
    
      function updateMail(frm, val) {
        frm.elements.mailto = addresses[val];
      }
    </script>
    <select onchange="updateMail(this.form, this.selectedIndex);" name="category">
      <option value="1">Category 1</option>
      <option value="2">Category 2</option>
    </select>
    However, a server-side solution would definitely be preferable -- it would neither break the script for non-JS users nor reveal the email addresses to potential spambots.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Feb 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hey Twey - that's very tight code.
    It looks like it's bang on too.

    I'll give that a go.

    Much appreciation! I'll report back soon.

  7. #7
    Join Date
    Feb 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hmmm. It doesn't seem to be populating the mailto field.

    Yes - I understand what you mean about server-side. This is more of a learning exercise for me though.
    I'll have to go over the code again to absorb it.

  8. #8
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    What is the "mailto field" ?
    - Mike

  9. #9
    Join Date
    Feb 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The mailto is an input field within the form.

  10. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Twey didn't specify an attribute to apply the value to... try replacing this line:
    Code:
      function updateMail(frm, val) {
        frm.elements.mailto = addresses[val];
      }
    with

    Code:
    frm.elements.mailto.value = addresses;[val]
    - Mike

Bookmarks

Posting Permissions

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