• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

Recipents based on multi-select box

Hi all ...

Here's one for you.

I have a multi-select box containing a list of stores. I have 5 people who each manage a set of stores. How would I easily manage this without 100 if then statements. Here's what I started thinking but then got stumped on how to proceed:

<select name="selStore" size="5" multiple id="selStore">
<option value="GR Clay">GR Clay</option>
<option value="Cascade">Cascade</option>
<option value="Lansing/Edgewood">Lansing/Edgewood</option>
<option value="Kazoo/Sprinkle Rd.">Kazoo/Sprinkle Rd.</option>
<option value="Saginaw">Saginaw</option>
<option value="Muskegon">Muskegon</option>
<option value="Bay City">Bay City</option>
<option value="Jackson">Jackson</option>
<option value="Battle Creek">Battle Creek</option>
<option value="Holland">Holland</option>
<option value="Traverse City">Traverse City</option>
<option value="Cadillac">Cadillac</option>
<option value="Sault Ste Marie">Sault Ste Marie</option>
<option value="Mt. Pleasant">Mt. Pleasant</option>
<option value="Gaylord">Gaylord</option>
<option value="Benton Harbor">Benton Harbor</option>
<option value="Kalamazoo M.H">Kalamazoo M.H</option>
<option value="Lansing Sag Hwy.">Lansing Sag Hwy.</option>
<option value="GR Alpine">GR Alpine</option>
<option value="Alpena">Alpena</option>
<option value="Marquette">Marquette</option>
<option value="Midland">Midland</option>
<option value="Jenison">Jenison</option>
<option value="Petoskey">Petoskey</option>
<option value="Escanaba">Escanaba</option>
</select>

Each user and the index number of each store they would be the recipent of the email if it was selected.

var MPContact1 = new Array(0,1,2,3,6)
var MPContact1Email = "email1@email.com"
var MPContact2 = new Array(4,5,7,8,9)
var MPContact2Email = "email2@email.com"
var MPContact3 = new Array(10,24,11,23,12)
var MPContact3Email = "email3@email.com"
var MPContact4 = new Array(13,14,15,16,17)
var MPContact4Email = "email4@email.com"
var MPContact5 = new Array(18,19,20,21,22)
var MPContact5Email = "email5@email.com"

When the user selects stores in the list, the recipient list is built bassed on which is selected.

Wow! Hard to explain by typing. It is in my head. I hope everyone understands.

Thanks in advance,
Chris24
0
Chris24
Asked:
Chris24
  • 3
  • 2
1 Solution
 
Chris24Author Commented:
Also, this is only a partial list. The actual list is over 120 items. And the numberr of people could grow or shrink from 2 people to possibly 10.

Thanks,
Chris
0
 
BatalfCommented:
This is one example - There's a lot of for-loops, but when a person is added to the recipient list this person will be skipped from the next iteration of the loop. So the loop doesn't take much time.

I have also used arrays for both contacts and contactIndexes.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
    <title>My test</title>    
    <script type="text/javascript">
   
    var contacts = new Array();
    var contactsIndexes = new Array();
    contacts[0] = "email1@email.com";
    contactsIndexes[0] = [0,1,2,3,6];
    contacts[1] = "email2@email.com";
    contactsIndexes[1] = [4,5,7,8,9];
    contacts[2] = "email3@email.com";
    contactsIndexes[2] = [10,24,11,23,12];
    contacts[3] = "email4@email.com";
    contactsIndexes[3] = [13,14,15,16,17];
    contacts[4] = "email5@email.com";
    contactsIndexes[4] = [18,19,20,21,22];    
   
    function sendMail(formObj){
        var sendTo = new Array();
        var sendToAssoc = new Array();
        var sendToString = "";
        var selectObj = formObj.selStore;
        for(var no=0;no<selectObj.options.length;no++){
            if(selectObj.options[no].selected){
                for(var no2=0;no2<contacts.length;no2++){
                    if(!sendToAssoc[no2]){
                        for(var no3=0;no3<contactsIndexes[no2].length;no3++){
                            if(contactsIndexes[no2][no3]==no){
                                if(sendToString.length>0)sendToString+=";";
                                sendToString+=contacts[no2];
                                sendToAssoc[no2]=true;
                                break;                                
                            }                            
                        }                        
                    }                    
                }                
            }            
        }
        location.href='mailto:'+sendToString;
    }
    </script>
    </head>
<body>
<form>
<select name="selStore" size="5" multiple id="selStore">
<option value="GR Clay">GR Clay</option>
<option value="Cascade">Cascade</option>
<option value="Lansing/Edgewood">Lansing/Edgewood</option>
<option value="Kazoo/Sprinkle Rd.">Kazoo/Sprinkle Rd.</option>
<option value="Saginaw">Saginaw</option>
<option value="Muskegon">Muskegon</option>
<option value="Bay City">Bay City</option>
<option value="Jackson">Jackson</option>
<option value="Battle Creek">Battle Creek</option>
<option value="Holland">Holland</option>
<option value="Traverse City">Traverse City</option>
<option value="Cadillac">Cadillac</option>
<option value="Sault Ste Marie">Sault Ste Marie</option>
<option value="Mt. Pleasant">Mt. Pleasant</option>
<option value="Gaylord">Gaylord</option>
<option value="Benton Harbor">Benton Harbor</option>
<option value="Kalamazoo M.H">Kalamazoo M.H</option>
<option value="Lansing Sag Hwy.">Lansing Sag Hwy.</option>
<option value="GR Alpine">GR Alpine</option>
<option value="Alpena">Alpena</option>
<option value="Marquette">Marquette</option>
<option value="Midland">Midland</option>
<option value="Jenison">Jenison</option>
<option value="Petoskey">Petoskey</option>
<option value="Escanaba">Escanaba</option>
</select>
<input type="button" onclick="sendMail(this.form)" value="Send email">


</form>
</body>
</html>
0
 
BatalfCommented:
Here's the function fromt he code above with some comments

    function sendMail(formObj){
        var sendToAssoc = new Array();
        var sendToString = "";  // Define variable that holds the email addresses
        var selectObj = formObj.selStore;   // Creating reference to select box, ( a shortcut)
        for(var no=0;no<selectObj.options.length;no++){    // Loop through select box options
            if(selectObj.options[no].selected){    // Is this particular option selected?
                for(var no2=0;no2<contacts.length;no2++){    // Loop through contact
                    if(!sendToAssoc[no2]){    // Is this contact allready added to the list, yes? -> Skip it
                        for(var no3=0;no3<contactsIndexes[no2].length;no3++){    // Loop through this contacts indexes
                            if(contactsIndexes[no2][no3]==no){    // This contact has a index matching the current select box index->Add him/her to the recipient list
                                if(sendToString.length>0)sendToString+=";";    // Adding semicolon to the lsit
                                sendToString+=contacts[no2];
                                sendToAssoc[no2]=true;    // Mark this contact as added to the list.
                                break;                                
                            }                            
                        }                        
                    }                    
                }                
            }            
        }
        location.href='mailto:'+sendToString;   // Send email
    }
0
 
Chris24Author Commented:
You rock Batalf!! I wish I could reward more just for how timely your solution was! I appreciate it!

Chris24
0
 
BatalfCommented:
Glad I could help!

Batalf
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now