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 8 of 8
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Insert a value(s) to input from a checkbox

    I've made an adressbook there you can tick of a checkbox if you want to insert the email adress that the box has as a value to an <input Type=text> field. The adressbook opens in a popup.
    So the script will insert the value from all the checked boxed (all named "to") and then close it self.

    I've tried many ways but none has worked properly.

    Any1 have an idea??

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,047
    Thanks
    0
    Thanked 251 Times in 247 Posts
    try:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function addEmails(oForm){
      var strEmails='';
      if (oForm.to.length){
        for (var i=0;i<oForm.to.length;i++){
          if (oForm.to[ i ].checked){
            if (strEmails!='') strEmails=','+oForm.to[ i ].value;
            else strEmails=oForm.to[ i ].value;
          }
        }
      }
      else if (oForm.to.checked) strEmails=oForm.to.value;
    
      if (opener && !opener.closed) {
        if (strEmails!='') opener.document.parentForm.parentTo.value = strEmails;
        else {
           alert("Nothing to add.");
           return;
        }
      } 
      else alert("Parent window does not exist. This popup will close.")
    
      window.close();
    }
    </script>
    </head>
    <body>
    <form>
    <input type="checkbox" name="to" value="email1@blah.com">email1@blah.com<br>
    <input type="checkbox" name="to" value="email2@blah.com">email2@blah.com<br>
    <input type="checkbox" name="to" value="email3@blah.com">email3@blah.com<br>
    <input type="checkbox" name="to" value="email4@blah.com">email4@blah.com<br>
    <input type="button" value="Add" onclick="addEmails(this.form)">
    <input type="button" value="Cancel" onclick="window.close()">
    </form>
    </body>
    </html>
    change parentForm to the name of the parent form and parentTo to the name of the field to insert email addrs to. I used comma as separator for the email addrs, change it accordingly.

  • #3
    New Coder
    Join Date
    Jul 2002
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeak it works...

    But, I've now got a problem, If I choose more then one email it will separate all the email with "," but only show the last email.

    Like this " , , , email@adress.com"

    And another question.
    I've got one more checkbox (cc). How do i implant that one to the script. I've tried to clone the script but then it won't work full (but still some )

    Thnx in before.

  • #4
    New Coder
    Join Date
    Jul 2002
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    any1?

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,047
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Forgot the +

    Code:
    if (strEmails!='') strEmails+=','+oForm.to[ i ].value;
    else strEmails=oForm.to[ i ].value;
    Extending the script to add CC and BCC fields..

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function addEmails(oForm){
      var strTO=getRecipients(oForm.to);
      var strCC=getRecipients(oForm.cc);
      var strBCC=getRecipients(oForm.bcc);
     
      if (opener && !opener.closed) {
        var parentF = opener.document.parentForm;
        if (strTO!='') parentF.parentTO.value = strTO;
        else if (strTo=='') {
           alert("Please select TO recipients");
           return;
        }
        if (strCC!='') parentF.parentCC.value = strCC;
        if (strBCC!='') parentF.parentBCC.value = strBCC;
        //also add checking for CC and BCC if they are required fields 
      } 
      else alert("Parent window does not exist. This popup will close.")
    
      window.close();
    }
    
    function getRecipients(oRecip){
      var strEmailAddrs='';
      if (oRecip.length){
        for (var i=0;i<oRecip.length;i++){
          if (oRecip[ i ].checked){
            if (strEmails!='') strEmailAddrs+=','+oRecip[ i ].value;
            else strEmailAddrs=oRecip[ i ].value;
          }
        }
      }
      else if (oRecip.checked) strEmailAddrs=oRecip.value;
      return strEmailAddrs;
    }
    </script>
    </head>
    <body>
    <form>
    <h4>TO:</h4>
    <input type="checkbox" name="to" value="email1@blah.com">email1@blah.com<br>
    <input type="checkbox" name="to" value="email2@blah.com">email2@blah.com<br>
    <input type="checkbox" name="to" value="email3@blah.com">email3@blah.com<br>
    <input type="checkbox" name="to" value="email4@blah.com">email4@blah.com<br>
    <h4>CC:</h4>
    <input type="checkbox" name="cc" value="email51@blah.com">email5@blah.com<br>
    <input type="checkbox" name="cc" value="email6@blah.com">email6@blah.com<br>
    <input type="checkbox" name="cc" value="email7@blah.com">email7@blah.com<br>
    <input type="checkbox" name="cc" value="email8@blah.com">email8@blah.com<br>
    <h4>BCC:</h4>
    <input type="checkbox" name="bcc" value="email9@blah.com">email9@blah.com<br>
    <input type="checkbox" name="bcc" value="email10@blah.com">email10@blah.com<br>
    <input type="checkbox" name="bcc" value="email11@blah.com">email11@blah.com<br>
    <input type="checkbox" name="bcc" value="email12@blah.com">email12@blah.com<br>
    <p>
    <input type="button" value="Add" onclick="addEmails(this.form)">
    <input type="button" value="Cancel" onclick="window.close()">
    </p>
    </form>
    </body>
    </html>
    change bold text to appropriate names in the parent window.

    not tested...

  • #6
    New Coder
    Join Date
    Jul 2002
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can you make the script disable all the other boxes if one of a type is checked so you can't check more than one adress. (e.g. 1 of To, 1 of CC and 1 of BCC)???

  • #7
    New Coder
    Join Date
    Jul 2002
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've tried a function that checks if a checkbox is checked, if it is, it will disable all with the same name. But that didn't work, I don't know if it was my code or something else.
    Last edited by Wobbler; 10-05-2003 at 09:09 AM.

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,047
    Thanks
    0
    Thanked 251 Times in 247 Posts
    huh? why won't you allow more than 1 email of each recipient type? Use radio button instead of checkbox then.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function addEmails(oForm){
      var strTO=getRecipient(oForm.to);
      var strCC=getRecipient(oForm.cc);
      var strBCC=getRecipient(oForm.bcc);
     
      if (opener && !opener.closed) {
        var parentF = opener.document.parentForm;
        if (strTO!='') parentF.parentTO.value = strTO;
        else if (strTo=='') {
           alert("Please select TO recipients");
           return;
        }
        if (strCC!='') parentF.parentCC.value = strCC;
        if (strBCC!='') parentF.parentBCC.value = strBCC;
        //also add checking for CC and BCC if they are required fields 
      } 
      else alert("Parent window does not exist. This popup will close.")
    
      window.close();
    }
    
    function getRecipient(oRecip){
      var strEmailAddr='';
      if (oRecip.length){
        for (var i=0;i<oRecip.length;i++){
          if (oRecip[ i ].checked){
            strEmailAddr=oRecip[ i ].value;
            break;
          }
        }
      }
      else if (oRecip.checked) strEmailAddr=oRecip.value;
      return strEmailAddr;
    }
    </script>
    </head>
    <body>
    <form>
    <h4>TO:</h4>
    <input type="radio" name="to" value="email1@blah.com">email1@blah.com<br>
    <input type="radio" name="to" value="email2@blah.com">email2@blah.com<br>
    <input type="radio" name="to" value="email3@blah.com">email3@blah.com<br>
    <input type="radio" name="to" value="email4@blah.com">email4@blah.com<br>
    <h4>CC:</h4>
    <input type="radio" name="cc" value="email51@blah.com">email5@blah.com<br>
    <input type="radio" name="cc" value="email6@blah.com">email6@blah.com<br>
    <input type="radio" name="cc" value="email7@blah.com">email7@blah.com<br>
    <input type="radio" name="cc" value="email8@blah.com">email8@blah.com<br>
    <h4>BCC:</h4>
    <input type="radio" name="bcc" value="email9@blah.com">email9@blah.com<br>
    <input type="radio" name="bcc" value="email10@blah.com">email10@blah.com<br>
    <input type="radio" name="bcc" value="email11@blah.com">email11@blah.com<br>
    <input type="radio" name="bcc" value="email12@blah.com">email12@blah.com<br>
    <p>
    <input type="button" value="Add" onclick="addEmails(this.form)">
    <input type="button" value="Cancel" onclick="window.close()">
    </p>
    </form>
    </body>
    </html>


  •  

    Posting Permissions

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