Multiple select box, pop up question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bonneylake
    Contributor
    • Aug 2008
    • 769

    Multiple select box, pop up question

    Hey Everyone,

    Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section.

    Basically i have a multiple select box. An it works except for this one part i want to add to it.The first box i have is called project members which shows the users you can choose to send an email to and the other box is called assignees which is the users that you selected to send an email to.

    What i am trying to do is instead of just choosing the users you want to send an email to and click submit. i want them to be required to select this check box that appears below it called assignees.If the check box is selected (and there are users in the assignees box) it will send an email to the the users selected when i click on submit.However, if the check box is not selected i need it be able to do 2 things. If there are users in the assignees box i need an error to pop up saying that they need to select the check box. But if there are no users in the assignees box then i need no errors to come up an just submit the form.

    Right now the only thing i have working is i can send an email to the user selected as long as the check box is selected. if the check box is not selected it will not send an email an you get a nice cold fusion error. Its mostly getting the pop up working that i am not sure about.

    But thank you in advance for all the help,
    Rach

    here is the form
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    <!---Multiple select box for assignees--->
    <script src="multipleselectbox.js"></script>
    
    <style type="text/css">
    
    
    	.multipleSelectBoxControl span{	/* Labels above select boxes*/
    		font-family: Tahoma;
    		font-size:13px;
    		font-weight: bold;
    	}
    	.multipleSelectBoxControl div select{	/* Select box layout */
    		font-family:arial;
    		height:100%;
    	}
    	.multipleSelectBoxControl input{	/* Small butons */
    		width:25px;	
    	}
    	
    	.multipleSelectBoxControl div{
    		float:left;
    	}
    		
    	.multipleSelectBoxDiv
    	</style>
        
    </head>
    
    <body>
    <!--- Assignees --->
          <form action="userform.cfm" id="userForm"  name="userForm" method="POST">                         
    Project Members:
    
    <select multiple name="fromBox" id="fromBox">
    	<option value="s@yahoo.com">Rachel</option>
    	<option value="p@Yahoo.com">Rae</option>
        <option value="r@hotmail.com">Work</option>
    </select>
    <select multiple name="toBox" id="toBox">
    
    </select>
    <script type="text/javascript">
    createMovableOptions("fromBox","toBox",350,150,'Project Members','Assignees');
    </script>
    <br/><br/>
    Send Email To:<br/>
    
    <input type="checkbox" name="assignees" id="assignees" value="" onclick="javascript:multipleSelectOnSubmit();"/>Assignees<br/>
    <input type="checkbox" name="contact" value=""/>Contact <br/>
    CC:<input type="text" name="contactemail" id="contactemail" value=""/><br/><br/>
    <input type="submit" class="officalsubmit" value="submit" name="submit" onClick="return validate_form();">
    </form>
    </body>
    </html>
    here is multipleselectb ox.js

    Code:
    var fromBoxArray = new Array();
    	var toBoxArray = new Array();
    	var selectBoxIndex = 0;
    	var arrayOfItemsToSelect = new Array();
    	
    	
    	function moveSingleElement()
    	{
    		var selectBoxIndex = this.parentNode.parentNode.id.replace(/[^\d]/g,'');
    		var tmpFromBox;
    		var tmpToBox;
    		if(this.tagName.toLowerCase()=='select'){			
    			tmpFromBox = this;
    			if(tmpFromBox==fromBoxArray[selectBoxIndex])tmpToBox = toBoxArray[selectBoxIndex]; else tmpToBox = fromBoxArray[selectBoxIndex];
    		}else{
    		
    			if(this.value.indexOf('>')>=0){
    				tmpFromBox = fromBoxArray[selectBoxIndex];
    				tmpToBox = toBoxArray[selectBoxIndex];			
    			}else{
    				tmpFromBox = toBoxArray[selectBoxIndex];
    				tmpToBox = fromBoxArray[selectBoxIndex];	
    			}
    		}
    		
    		for(var no=0;no<tmpFromBox.options.length;no++){
    			if(tmpFromBox.options[no].selected){
    				tmpFromBox.options[no].selected = false;
    				tmpToBox.options[tmpToBox.options.length] = new Option(tmpFromBox.options[no].text,tmpFromBox.options[no].value);
    				
    				for(var no2=no;no2<(tmpFromBox.options.length-1);no2++){
    					tmpFromBox.options[no2].value = tmpFromBox.options[no2+1].value;
    					tmpFromBox.options[no2].text = tmpFromBox.options[no2+1].text;
    					tmpFromBox.options[no2].selected = tmpFromBox.options[no2+1].selected;
    				}
    				no = no -1;
    				tmpFromBox.options.length = tmpFromBox.options.length-1;
    											
    			}			
    		}
    		
    		
    		var tmpTextArray = new Array();
    		for(var no=0;no<tmpFromBox.options.length;no++){
    			tmpTextArray.push(tmpFromBox.options[no].text + '___' + tmpFromBox.options[no].value);			
    		}
    		tmpTextArray.sort();
    		var tmpTextArray2 = new Array();
    		for(var no=0;no<tmpToBox.options.length;no++){
    			tmpTextArray2.push(tmpToBox.options[no].text + '___' + tmpToBox.options[no].value);			
    		}		
    		tmpTextArray2.sort();
    		
    		for(var no=0;no<tmpTextArray.length;no++){
    			var items = tmpTextArray[no].split('___');
    			tmpFromBox.options[no] = new Option(items[0],items[1]);
    			
    		}		
    		
    		for(var no=0;no<tmpTextArray2.length;no++){
    			var items = tmpTextArray2[no].split('___');
    			tmpToBox.options[no] = new Option(items[0],items[1]);			
    		}
    	}
    	
    	function sortAllElement(boxRef)
    	{
    		var tmpTextArray2 = new Array();
    		for(var no=0;no<boxRef.options.length;no++){
    			tmpTextArray2.push(boxRef.options[no].text + '___' + boxRef.options[no].value);			
    		}		
    		tmpTextArray2.sort();		
    		for(var no=0;no<tmpTextArray2.length;no++){
    			var items = tmpTextArray2[no].split('___');
    			boxRef.options[no] = new Option(items[0],items[1]);			
    		}		
    		
    	}
    	function moveAllElements()
    	{
    		var selectBoxIndex = this.parentNode.parentNode.id.replace(/[^\d]/g,'');
    		var tmpFromBox;
    		var tmpToBox;		
    		if(this.value.indexOf('>')>=0){
    			tmpFromBox = fromBoxArray[selectBoxIndex];
    			tmpToBox = toBoxArray[selectBoxIndex];			
    		}else{
    			tmpFromBox = toBoxArray[selectBoxIndex];
    			tmpToBox = fromBoxArray[selectBoxIndex];	
    		}
    		
    		for(var no=0;no<tmpFromBox.options.length;no++){
    			tmpToBox.options[tmpToBox.options.length] = new Option(tmpFromBox.options[no].text,tmpFromBox.options[no].value);			
    		}	
    		
    		tmpFromBox.options.length=0;
    		sortAllElement(tmpToBox);
    		
    	}
    	
    	
    	/* This function highlights options in the "to-boxes". It is needed if the values should be remembered after submit. Call this function onsubmit for your form */
    	function multipleSelectOnSubmit()
    	{
    		for(var no=0;no<arrayOfItemsToSelect.length;no++){
    			var obj = arrayOfItemsToSelect[no];
    			for(var no2=0;no2<obj.options.length;no2++){
    
    				obj.options[no2].selected = true;
    			}
    		}
    		
    	}
    	
    	function createMovableOptions(fromBox,toBox,totalWidth,totalHeight,labelLeft,labelRight)
    	{		
    		fromObj = document.getElementById(fromBox);
    		toObj = document.getElementById(toBox);
    		
    		arrayOfItemsToSelect[arrayOfItemsToSelect.length] = toObj;
    
    		
    		fromObj.ondblclick = moveSingleElement;
    		toObj.ondblclick = moveSingleElement;
    
    		
    		fromBoxArray.push(fromObj);
    		toBoxArray.push(toObj);
    		
    		var parentEl = fromObj.parentNode;
    		
    		var parentDiv = document.createElement('DIV');
    		parentDiv.className='multipleSelectBoxControl';
    		parentDiv.id = 'selectBoxGroup' + selectBoxIndex;
    		parentDiv.style.width = totalWidth + 'px';
    		parentDiv.style.height = totalHeight + 'px';
    		parentEl.insertBefore(parentDiv,fromObj);
    		
    		
    		var subDiv = document.createElement('DIV');
    		subDiv.style.width = (Math.floor(totalWidth/2) - 15) + 'px';
    		fromObj.style.width = (Math.floor(totalWidth/2) - 15) + 'px';
    
    		var label = document.createElement('SPAN');
    		label.innerHTML = labelLeft;
    		subDiv.appendChild(label);
    		
    		subDiv.appendChild(fromObj);
    		subDiv.className = 'multipleSelectBoxDiv';
    		parentDiv.appendChild(subDiv);
    		
    		
    		var buttonDiv = document.createElement('DIV');
    		buttonDiv.style.verticalAlign = 'middle';
    		buttonDiv.style.paddingTop = (totalHeight/2) - 50 + 'px';
    		buttonDiv.style.width = '30px';
    		buttonDiv.style.textAlign = 'center';
    		parentDiv.appendChild(buttonDiv);
    		
    		var buttonRight = document.createElement('INPUT');
    		buttonRight.type='button';
    		buttonRight.value = '>';
    		buttonDiv.appendChild(buttonRight);	
    		buttonRight.onclick = moveSingleElement;	
    		
    		var buttonAllRight = document.createElement('INPUT');
    		buttonAllRight.type='button';
    		buttonAllRight.value = '>>';
    		buttonAllRight.onclick = moveAllElements;
    		buttonDiv.appendChild(buttonAllRight);		
    		
    		var buttonLeft = document.createElement('INPUT');
    		buttonLeft.style.marginTop='10px';
    		buttonLeft.type='button';
    		buttonLeft.value = '<';
    		buttonLeft.onclick = moveSingleElement;
    		buttonDiv.appendChild(buttonLeft);		
    		
    		var buttonAllLeft = document.createElement('INPUT');
    		buttonAllLeft.type='button';
    		buttonAllLeft.value = '<<';
    		buttonAllLeft.onclick = moveAllElements;
    		buttonDiv.appendChild(buttonAllLeft);
    		
    		var subDiv = document.createElement('DIV');
    		subDiv.style.width = (Math.floor(totalWidth/2) - 15) + 'px';
    		toObj.style.width = (Math.floor(totalWidth/2) - 15) + 'px';
    
    		var label = document.createElement('SPAN');
    		label.innerHTML = labelRight;
    		subDiv.appendChild(label);
    				
    		subDiv.appendChild(toObj);
    		parentDiv.appendChild(subDiv);		
    		
    		toObj.style.height = (totalHeight - label.offsetHeight) + 'px';
    		fromObj.style.height = (totalHeight - label.offsetHeight) + 'px';
    
    			
    		selectBoxIndex++;
    		
    	}
    an here is the userform.cfm page

    Code:
    <cfmail to="#form.toBox#"
    	from="Customer Support Email Alert <Reports@howardcomputers.com>"
    	subject="Customer Support Email Notification"
    	type="HTML">
    	<cfinclude template="printticket.cfm">
    </cfmail>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Create a validation function to call in the form onsubmit event:
    [code=html]<form ... onsubmit="retur n validate()">[/code]In the validate() function, perform your validation. First get the checkbox element. Use the checked property to check if it has been checked. Access the Assignees box element and loop through the options to see if any are selected. If not, you can use alert() to display a message. Alternatively, you can just display an error message next to the element using DOM methods. If it passes validation, return true to submit and false otherwise to prevent submission.

    Comment

    • bonneylake
      Contributor
      • Aug 2008
      • 769

      #3
      Hey Acoder,

      well i was wondering could i just put it with another validation page i have now?
      the original form is to long so i shorted it to show what i was working with. but anyway on my original form page i have this line

      Code:
      <input type="submit" class="officalsubmit" value="submit" name="submit" onClick="return validate_form();">
      an was wondering if that was ok to leave it like that for validation?

      an well this is what appears on my validation page.

      Code:
      function validate_form()
      {
      //VALIDATES HARDWARE
      var hardware = document.getElementById('hardwarefailure')
      var tag = document.getElementsByTagName('select')
      
      for(var i=0; i<=tag.length-1; i++){
      //check each tag (tag[i]) to see if its hardware failure
       //if it is, then see if its value is nothing
       //Rachels Note (had to use name== instead of id because id is unique and the select each item does not need to be unique)
      if(tag[i].name=="hardwarefailure" && tag[i].value =='')
      {
      	alert ('Please Select Type of Hardware Failure');
      	return false;
      }
      }
      //VALIDATES DEPT/VENDOR RESPONSIBILTYS
      var dept = document.getElementById('deptvendor')
      var tag2 = document.getElementsByTagName('select')
      
      for(var i=0; i<=tag2.length-1; i++){
      //check each tag (tag[i]) to see if its hardware failure
       //if it is, then see if its value is nothing
      if(tag2[i].name=="deptvendor" && tag2[i].value =='')
      {
      	alert ('Please Select Dept/Vendor Responsibility');
      	return false;
      }
      }
      //VALIDATES PARTS RETURNED
      var parts = document.getElementById('partsreturn')
      var tag3 = document.getElementsByTagName('select')
      
      for(var i=0; i<=tag3.length-1; i++){
      //check each tag (tag[i]) to see if its hardware failure
       //if it is, then see if its value is nothing
      if(tag3[i].name=="partsreturn" && tag3[i].value =='')
      {
      	alert ('Please Select Have all parts been returned');
      	return false;
      }
      }
      
      if(softhardware.value == ''){
      softhardware.value = 'NOW IT IS THIS' ;
      return false;
      }
      
      
      
      
      
      
      return true;
      }

      but i am not sure how to check to see if the checkbox is checked. i have done a select an a input. but how would i do a checkbox?

      Thank you again for all the help :),
      Rach

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You can add it to the current validate function.

        To validate checkboxes, you'd normally loop over them, but if you only have one, you can just check it directly, e.g.
        [code=javascript]var chkbox = document.getEle mentById("chkbo xID");
        if (chkbox.checked ) {
        //checkbox is checked...
        } else {
        // checkbox is not checked...[/code]

        Comment

        • bonneylake
          Contributor
          • Aug 2008
          • 769

          #5
          Hey Acoder,

          Well i was on the right track that means. Figured out if the check box is not checked an had it displaying an alert, got that far. but anyway i changed mine to yours an this is what i have.

          Code:
          var chkbox = document.getElementById("assignees");
          if (chkbox.checked) {
          	
          }else {
          	 alert ( "Checkbox 'assignees' must be selected in order to send email to assignees" );
                  return false;
          }
          but how do i add the part about the tobox?

          Thank you :),
          Rach

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            If you've not got anything inside the if, it could be shortened to:
            [code=javascript]if (!chkbox.checke d) {
            alert ( "Checkbox 'assignees' must be selected in order to send email to assignees" );[/code]
            For the multiple select box, access it by ID (or name), then use a for loop to loop over its options using the options array. This time you're checking the selected property.

            Comment

            • bonneylake
              Contributor
              • Aug 2008
              • 769

              #7
              Hey Acoder,

              well i tried this, but its not working correctly. Did i do it wrong? heres the whole thing

              Code:
              var chkbox = document.getElementById("assignees");
              if (chkbox.checked) {
              
              }else {
              	 alert ( "Checkbox 'assignees' must be selected in order to send email to assignees" );
                      return false;
              }
              
              
              var selected = document.getElementById('toBox')
              var tag4 = document.getElementsByTagName('select')
              
              for(var i=0; i<=tag4.length-1; i++){
              //check each tag (tag[i]) to see if its hardware failure
               //if it is, then see if its value is nothing
              if(tag4[i].name=="toBox" && tag4[i].value =='')
              {
              	alert ('Please Select Dept/Vendor Responsibility');
              	return false;
              }
              }
              Thank you :),
              Rach

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Line 10 is the correct way to access it - get it directly rather than looping over all the select elements.

                Then loop over selected.option s. Check for options[i].selected.

                Comment

                • bonneylake
                  Contributor
                  • Aug 2008
                  • 769

                  #9
                  Hey Acoder,

                  i am still doing something wrong,just not sure what. here is what i got


                  Code:
                  var selected = document.getElementById('toBox')
                  
                  for(var i=0; i<=selected.options[i]; i++){
                  //check each tag (tag[i]) to see if its hardware failure
                   //if it is, then see if its value is nothing
                  if(selected.options[i].name=="toBox" && selected.options[i].value =='')
                  {
                  	alert ('Please Select a assignee');
                  	return false;
                  }
                  }
                  Thank you,
                  Rach

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    You need to use another variable (boolean) and set it to false initially and true when you find an option selected. You also have problems in the for loop:
                    Code:
                    var selected = document.getElementById('toBox')
                    var isSelected = false;
                    for(var i = 0; i < selected.options.length; i++){
                        if (selected.options[i].selected) {
                    ...

                    Comment

                    • bonneylake
                      Contributor
                      • Aug 2008
                      • 769

                      #11
                      Hey Acoder,

                      Still missing something because when click submit now i get no errors an just takes me to the next page with or without the check box clicked an with or without anything in the multiple select box.

                      Code:
                      var chkbox = document.getElementById("assignees");
                      if (chkbox.checked) {
                      
                      }else {
                      	 alert ( "Checkbox 'assignees' must be selected in order to send email to assignees" );
                              return false;
                      }
                      
                      var selected = document.getElementById('toBox')
                      var isSelected = false;
                      for(var i=0; i<=selected.options.length; i++){
                      if(selected.options[i].selected)
                      {
                      	
                      }else{
                      	alert ("Please Select a assignee");
                      	return false;
                      }
                      
                      
                      return true;
                      }
                      Thank you,
                      Rach

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        The alert has to be outside the loop. That's the whole point of the isSelected variable. If any option is selected, you set it to true in the loop. Outside the loop, check that isSelected is not true and display an alert:
                        [code=javascript]if (!isSelected) {
                        alert(...[/code]

                        Comment

                        • bonneylake
                          Contributor
                          • Aug 2008
                          • 769

                          #13
                          Hey Acoder,

                          I thought i understood what you said but i think i might of gotten confused. Here is what i tried

                          Code:
                          var chkbox = document.getElementById("assignees");
                          if (chkbox.checked) {
                          }else {
                          	 alert ( "Checkbox 'assignees' must be selected in order to send email to assignees" );
                                  return false;
                          }
                          
                          var selected = document.getElementById('toBox')
                          var isSelected = false;
                          for(var i=0; i<=selected.options.length; i++){
                          if(selected.options[i].selected){	
                          }	
                          }
                          if (!isSelected){
                          	alert ("Please Select a assignee");
                          	return false;}
                          	
                          
                          
                          return true;
                          }
                          Thank you,
                          Rach

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            You have to set isSelected to true in the if statement and the for loop should loop from 0 to length-1:
                            Code:
                            for(var i=0; i<=selected.options.length-1; i++){
                                if(selected.options[i].selected){
                                    isSelected = true;
                                    break;
                                }	
                            }

                            Comment

                            • bonneylake
                              Contributor
                              • Aug 2008
                              • 769

                              #15
                              Hey Acoder,

                              here is what i got. on the if (!isSelected) do i need to change the if to an else? because the checkbox is working right now but the other part is not working.

                              Code:
                              var chkbox = document.getElementById("assignees");
                              if (chkbox.checked) {
                              }else {
                              	 alert ( "Checkbox 'assignees' must be selected in order to send email to assignees" );
                                      return false;
                              }
                              
                              var selected = document.getElementById("toBox")
                              var isSelected = false;
                              for(var i=0; i<=selected.options.length-1; i++){
                              if(selected.options[i].selected ){	
                              isSelected = true;
                              break;
                              }	
                              }
                              if (!isSelected){
                              	alert ("Please Select a assignee");
                              	return false;}
                              	
                              
                              
                              return true;
                              }
                              Thank you,
                              Rach

                              Comment

                              Working...