Problem to transfer selected multiple value from select box to PHP page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imrantbd
    New Member
    • Aug 2006
    • 9

    Problem to transfer selected multiple value from select box to PHP page

    I need array type name like "destList[]" must use for my destlist select box,not a single name.Or need a solution to capture multiple value of "destList[]" select box and send all selected value in php page.The multiple select value then insert in database added by comma.The following is my code:

    Form Page:form.php
    Code:
       
    <head>
    <script language="JavaScript">
    function addSrcToDestList() {
     
    destList1 = window.document.forms[0].destList;
    srcList = window.document.forms[0].srcList;
    var len = destList1.length;
    for(var i = 0; i < srcList.length; i++) {
    if ((srcList.options[i] != null) && (srcList.options[i].selected)) {
    //Check if this value already exist in the destList or not
    //if not then add it otherwise do not add it.
    var found = false;
    for(var count = 0; count < len; count++) {
    if (destList1.options[count] != null) {
    if (srcList.options[i].text == destList1.options[count].text) {
    found = true;
    break;
    }
    }
    }
     
    if (found != true) {
    destList1.options[len] = new Option(srcList.options[i].text);
    len++;
    }
    }
    }
    }
     
    // Deletes from the destination list.
    function deleteFromDestList() {
    var destList1 = window.document.forms[0].destList;
    var len = destList1.options.length;
    for(var i = (len-1); i >= 0; i--) {
    if ((destList1.options[i] != null) && (destList1.options[i].selected == true)) {
    destList1.options[i] = null;
    }
    }
    }
     
     
    function allSelect()
    {
    List = document.forms[0].destList;
    if (List.length && List.options[0].value == 'temp') return;
    for (i=0;i<List.length;i++)
    {
    List.options[i].selected = true;
    }
    }
     
    </SCRIPT>
    </head>
     
    <body>
    <form name="upload" id="upload" ENCTYPE="multipart/form-data" method="POST" action="<?php $_SERVER['PHP_SELF']?>" onSubmit="allSelect();">
     
    <select name="srcList" multiple size=10 style="width:150;" onChange="moveOver();">
    <option value ="0" >Admin</option>
    <option value ="1" >Public</option>
    <option value ="2" >Private</option>
    <option value ="3" >All</option>
    </select>
     
    <input type="button" value=" >> " onClick="javascript:addSrcToDestList()">
    <br>
    <br>
    <input type="button" value=" << " onClick="javascript:deleteFromDestList();">
     
    <select name="destList[]" size=10 style="width:150;" multiple >
    </select>
     
    <input type="submit" class="button3" name="upload" value="Insert" >
     
    </body>
    PHP page: process.php
    Code:
    <?
    $result2 = mysql_query("insert into
    ccms_table values(' " . join(",",$_POST["destList"]) ." ' ) );
    ?>
    if I use the name "destList" then I can not transfer & capture multiple value in php page.So I need "destList[]" type name in select box.On the other hand if I use "destList[]" I can transfer multi value but the array type name "destList[]" does not work in java script code.This is my main problem.On java script side I need to use single name like "destList" & on PHP side I need to use array type name like "destList[]".

    Please give the solution.Thanks to all advance.
    Last edited by Niheel; Jan 10 '12, 06:49 PM.
  • timhussey
    New Member
    • Aug 2006
    • 1

    #2
    Here try this code,
    I think this is what you were trying to do...

    I left some comments in the code to try and help you understand what
    some of the code is doing....

    let me know if this helps...


    Code:
    <head>
    <script language="JavaScript">
    
    function move(fbox, tbox) {
    
    	var arrFbox = new Array();
    	var arrTbox = new Array();
    	var arrLookup = new Array();
    	var i;
    	
    	//////////////////////////////////////////////////////////////////////
    	//////////////////////////////////////////////////////////////////////
    	/////  This for loop gets the length from the destination field  /////
    	//////////////////////////////////////////////////////////////////////
    	////////////////////////////////////////////////////////////////////// 
    	for (i = 0; i < tbox.options.length; i++) {
    	
    		arrLookup[tbox.options[i].text] = tbox.options[i].value;
    		arrTbox[i] = tbox.options[i].text;
    		
    	}
    	//////////////////////////////////////////////////////////////////////
    	//////////////////////////////////////////////////////////////////////
    	
    
    	var fLength = 0;
    	var tLength = arrTbox.length;
    	
    	
    	//////////////////////////////////////////////////////////////////////
    	//////////////////////////////////////////////////////////////////////
    	/////  This for loop gets the length of the source field  ////////////
    	//////////////////////////////////////////////////////////////////////
    	//////////////////////////////////////////////////////////////////////
    	for(i = 0; i < fbox.options.length; i++) {
    	
    			arrLookup[fbox.options[i].text] = fbox.options[i].value;
    			
    				//////////////////////////////////////////////////////////////////////
    				//////////////////////////////////////////////////////////////////////
    				/////  If if the item is selected and not empty  /////////////////////
    				//////////////////////////////////////////////////////////////////////
    				//////////////////////////////////////////////////////////////////////
    				if (fbox.options[i].selected && fbox.options[i].value != "") {
    				
    						arrTbox[tLength] = fbox.options[i].text;
    						tLength++;
    					
    				} else {
    				
    						arrFbox[fLength] = fbox.options[i].text;
    						fLength++;
    					
    				}
    				//////////////////////////////////////////////////////////////////////
    				//////////////////////////////////////////////////////////////////////
    				
    	}
    	//////////////////////////////////////////////////////////////////////
    	//////////////////////////////////////////////////////////////////////
    	
    	//////////////////////////////////////////////////////////////////////
    	//////////////////////////////////////////////////////////////////////
    	/////  This alphabetically sorts the array of the items  /////////////
    	//////////////////////////////////////////////////////////////////////
    	//////////////////////////////////////////////////////////////////////
    	
    		//arrFbox.sort();
    		//arrTbox.sort();
    		fbox.length = 0;
    		tbox.length = 0;
    		var c;
    		
    		//////////////////////////////////////////////////////////////////////
    		//////////////////////////////////////////////////////////////////////
    		/////  now transfer the items stored in the arrFbox[] array  /////////
    		//////////////////////////////////////////////////////////////////////
    		//////////////////////////////////////////////////////////////////////
    		for(c = 0; c < arrFbox.length; c++) {
    		
    				var no = new Option();
    				no.value = arrLookup[arrFbox[c]];
    				no.text = arrFbox[c];
    				fbox[c] = no;
    				
    		}
    		//////////////////////////////////////////////////////////////////////
    		//////////////////////////////////////////////////////////////////////
    		
    		//////////////////////////////////////////////////////////////////////
    		//////////////////////////////////////////////////////////////////////
    		/////  now send all the items to the destination field  //////////////
    		//////////////////////////////////////////////////////////////////////
    		//////////////////////////////////////////////////////////////////////
    		for(c = 0; c < arrTbox.length; c++) {
    		
    				var no = new Option();
    				no.value = arrLookup[arrTbox[c]];
    				no.text = arrTbox[c];
    				tbox[c] = no;
    								
    		}
    		//////////////////////////////////////////////////////////////////////
    		//////////////////////////////////////////////////////////////////////
    		
    }
    
    	//////////////////////////////////////////////////////////////////////
    	//////////////////////////////////////////////////////////////////////
    	
    	function addMe() {
    	
    	
    		 theValues = document.combo_box.list2;
    		 
    		 for(i = 0; i < theValues.length; i++) {
    		
    				
    				//alert(theValues[i].value);
    				if (   document.combo_box.theNumbers.value == ""   ) {
    					document.combo_box.theNumbers.value = theValues[i].value;
    				} else {
    					document.combo_box.theNumbers.value += "," + theValues[i].value;
    				}
    				
    				
    								
    		}
    		
    			return true;
    	
    	}
    	
    	//////////////////////////////////////////////////////////////////////
    	//////////////////////////////////////////////////////////////////////
    
    
    </SCRIPT>
    </head>
    <body>
    <form name="upload" id="upload" method="POST" action="process.php" onSubmit="return addMe();">
    <table><tr><td>
      <select name="list1" id="list1" multiple size=10 style="width:150;" >
        <option value ="0" >Admin</option>
        <option value ="1" >Public</option>
        <option value ="2" >Private</option>
        <option value ="3" >All</option>
      </select>
      </td>
      <td align="center" valign="middle"><input type="button" class="normalContent" onClick="move(this.form.list2,this.form.list1)" value="<<">
        <input type="button" class="normalContent" onClick="move(this.form.list1,this.form.list2)" value=">>">
      </td>
      <td><select name="list2" id="list2" size="10" multiple class="normalContent" style="width:150px;">
        </select>
      </td>
      </tr>
      </table>
      <div align="center">
        <input id="theNumbers" name="theNumbers" type="hidden" />
        <input name="submit" type="submit" class="checkFormSubmit" id="submit" value="Upload" />
      </div>
    </form>
    </body>

    <?=$Tim_Hussey; ?>

    Comment

    • imrantbd
      New Member
      • Aug 2006
      • 9

      #3
      This is a nice way to solve my problem.Many thanks to timhussey.

      Comment

      • hamzatki
        New Member
        • Feb 2007
        • 1

        #4
        Hi imrantbd,

        I have similar problems with my mutiple selection field.

        I just want to out if the solution from Timhussey really solved your problem and what changes do you do to your process.php.

        Many Thanks.

        Kamal.

        Comment

        Working...