473,390 Members | 1,429 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1.  
  2. <head>
  3. <script language="JavaScript">
  4. function addSrcToDestList() {
  5.  
  6. destList1 = window.document.forms[0].destList;
  7. srcList = window.document.forms[0].srcList;
  8. var len = destList1.length;
  9. for(var i = 0; i < srcList.length; i++) {
  10. if ((srcList.options[i] != null) && (srcList.options[i].selected)) {
  11. //Check if this value already exist in the destList or not
  12. //if not then add it otherwise do not add it.
  13. var found = false;
  14. for(var count = 0; count < len; count++) {
  15. if (destList1.options[count] != null) {
  16. if (srcList.options[i].text == destList1.options[count].text) {
  17. found = true;
  18. break;
  19. }
  20. }
  21. }
  22.  
  23. if (found != true) {
  24. destList1.options[len] = new Option(srcList.options[i].text);
  25. len++;
  26. }
  27. }
  28. }
  29. }
  30.  
  31. // Deletes from the destination list.
  32. function deleteFromDestList() {
  33. var destList1 = window.document.forms[0].destList;
  34. var len = destList1.options.length;
  35. for(var i = (len-1); i >= 0; i--) {
  36. if ((destList1.options[i] != null) && (destList1.options[i].selected == true)) {
  37. destList1.options[i] = null;
  38. }
  39. }
  40. }
  41.  
  42.  
  43. function allSelect()
  44. {
  45. List = document.forms[0].destList;
  46. if (List.length && List.options[0].value == 'temp') return;
  47. for (i=0;i<List.length;i++)
  48. {
  49. List.options[i].selected = true;
  50. }
  51. }
  52.  
  53. </SCRIPT>
  54. </head>
  55.  
  56. <body>
  57. <form name="upload" id="upload" ENCTYPE="multipart/form-data" method="POST" action="<?php $_SERVER['PHP_SELF']?>" onSubmit="allSelect();">
  58.  
  59. <select name="srcList" multiple size=10 style="width:150;" onChange="moveOver();">
  60. <option value ="0" >Admin</option>
  61. <option value ="1" >Public</option>
  62. <option value ="2" >Private</option>
  63. <option value ="3" >All</option>
  64. </select>
  65.  
  66. <input type="button" value=" >> " onClick="javascript:addSrcToDestList()">
  67. <br>
  68. <br>
  69. <input type="button" value=" << " onClick="javascript:deleteFromDestList();">
  70.  
  71. <select name="destList[]" size=10 style="width:150;" multiple >
  72. </select>
  73.  
  74. <input type="submit" class="button3" name="upload" value="Insert" >
  75.  
  76. </body>
  77.  
  78.  
PHP page: process.php
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $result2 = mysql_query("insert into
  3. ccms_table values(' " . join(",",$_POST["destList"]) ." ' ) );
  4. ?>
  5.  
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.
Aug 31 '06 #1
3 8689
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...


Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <script language="JavaScript">
  3.  
  4. function move(fbox, tbox) {
  5.  
  6.     var arrFbox = new Array();
  7.     var arrTbox = new Array();
  8.     var arrLookup = new Array();
  9.     var i;
  10.  
  11.     //////////////////////////////////////////////////////////////////////
  12.     //////////////////////////////////////////////////////////////////////
  13.     /////  This for loop gets the length from the destination field  /////
  14.     //////////////////////////////////////////////////////////////////////
  15.     ////////////////////////////////////////////////////////////////////// 
  16.     for (i = 0; i < tbox.options.length; i++) {
  17.  
  18.         arrLookup[tbox.options[i].text] = tbox.options[i].value;
  19.         arrTbox[i] = tbox.options[i].text;
  20.  
  21.     }
  22.     //////////////////////////////////////////////////////////////////////
  23.     //////////////////////////////////////////////////////////////////////
  24.  
  25.  
  26.     var fLength = 0;
  27.     var tLength = arrTbox.length;
  28.  
  29.  
  30.     //////////////////////////////////////////////////////////////////////
  31.     //////////////////////////////////////////////////////////////////////
  32.     /////  This for loop gets the length of the source field  ////////////
  33.     //////////////////////////////////////////////////////////////////////
  34.     //////////////////////////////////////////////////////////////////////
  35.     for(i = 0; i < fbox.options.length; i++) {
  36.  
  37.             arrLookup[fbox.options[i].text] = fbox.options[i].value;
  38.  
  39.                 //////////////////////////////////////////////////////////////////////
  40.                 //////////////////////////////////////////////////////////////////////
  41.                 /////  If if the item is selected and not empty  /////////////////////
  42.                 //////////////////////////////////////////////////////////////////////
  43.                 //////////////////////////////////////////////////////////////////////
  44.                 if (fbox.options[i].selected && fbox.options[i].value != "") {
  45.  
  46.                         arrTbox[tLength] = fbox.options[i].text;
  47.                         tLength++;
  48.  
  49.                 } else {
  50.  
  51.                         arrFbox[fLength] = fbox.options[i].text;
  52.                         fLength++;
  53.  
  54.                 }
  55.                 //////////////////////////////////////////////////////////////////////
  56.                 //////////////////////////////////////////////////////////////////////
  57.  
  58.     }
  59.     //////////////////////////////////////////////////////////////////////
  60.     //////////////////////////////////////////////////////////////////////
  61.  
  62.     //////////////////////////////////////////////////////////////////////
  63.     //////////////////////////////////////////////////////////////////////
  64.     /////  This alphabetically sorts the array of the items  /////////////
  65.     //////////////////////////////////////////////////////////////////////
  66.     //////////////////////////////////////////////////////////////////////
  67.  
  68.         //arrFbox.sort();
  69.         //arrTbox.sort();
  70.         fbox.length = 0;
  71.         tbox.length = 0;
  72.         var c;
  73.  
  74.         //////////////////////////////////////////////////////////////////////
  75.         //////////////////////////////////////////////////////////////////////
  76.         /////  now transfer the items stored in the arrFbox[] array  /////////
  77.         //////////////////////////////////////////////////////////////////////
  78.         //////////////////////////////////////////////////////////////////////
  79.         for(c = 0; c < arrFbox.length; c++) {
  80.  
  81.                 var no = new Option();
  82.                 no.value = arrLookup[arrFbox[c]];
  83.                 no.text = arrFbox[c];
  84.                 fbox[c] = no;
  85.  
  86.         }
  87.         //////////////////////////////////////////////////////////////////////
  88.         //////////////////////////////////////////////////////////////////////
  89.  
  90.         //////////////////////////////////////////////////////////////////////
  91.         //////////////////////////////////////////////////////////////////////
  92.         /////  now send all the items to the destination field  //////////////
  93.         //////////////////////////////////////////////////////////////////////
  94.         //////////////////////////////////////////////////////////////////////
  95.         for(c = 0; c < arrTbox.length; c++) {
  96.  
  97.                 var no = new Option();
  98.                 no.value = arrLookup[arrTbox[c]];
  99.                 no.text = arrTbox[c];
  100.                 tbox[c] = no;
  101.  
  102.         }
  103.         //////////////////////////////////////////////////////////////////////
  104.         //////////////////////////////////////////////////////////////////////
  105.  
  106. }
  107.  
  108.     //////////////////////////////////////////////////////////////////////
  109.     //////////////////////////////////////////////////////////////////////
  110.  
  111.     function addMe() {
  112.  
  113.  
  114.          theValues = document.combo_box.list2;
  115.  
  116.          for(i = 0; i < theValues.length; i++) {
  117.  
  118.  
  119.                 //alert(theValues[i].value);
  120.                 if (   document.combo_box.theNumbers.value == ""   ) {
  121.                     document.combo_box.theNumbers.value = theValues[i].value;
  122.                 } else {
  123.                     document.combo_box.theNumbers.value += "," + theValues[i].value;
  124.                 }
  125.  
  126.  
  127.  
  128.         }
  129.  
  130.             return true;
  131.  
  132.     }
  133.  
  134.     //////////////////////////////////////////////////////////////////////
  135.     //////////////////////////////////////////////////////////////////////
  136.  
  137.  
  138. </SCRIPT>
  139. </head>
  140. <body>
  141. <form name="upload" id="upload" method="POST" action="process.php" onSubmit="return addMe();">
  142. <table><tr><td>
  143.   <select name="list1" id="list1" multiple size=10 style="width:150;" >
  144.     <option value ="0" >Admin</option>
  145.     <option value ="1" >Public</option>
  146.     <option value ="2" >Private</option>
  147.     <option value ="3" >All</option>
  148.   </select>
  149.   </td>
  150.   <td align="center" valign="middle"><input type="button" class="normalContent" onClick="move(this.form.list2,this.form.list1)" value="<<">
  151.     <input type="button" class="normalContent" onClick="move(this.form.list1,this.form.list2)" value=">>">
  152.   </td>
  153.   <td><select name="list2" id="list2" size="10" multiple class="normalContent" style="width:150px;">
  154.     </select>
  155.   </td>
  156.   </tr>
  157.   </table>
  158.   <div align="center">
  159.     <input id="theNumbers" name="theNumbers" type="hidden" />
  160.     <input name="submit" type="submit" class="checkFormSubmit" id="submit" value="Upload" />
  161.   </div>
  162. </form>
  163. </body>

<?=$Tim_Hussey;?>
Aug 31 '06 #2
This is a nice way to solve my problem.Many thanks to timhussey.
Sep 2 '06 #3
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.
Feb 12 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Munzilla | last post by:
Ok, I have an ASP page that I use to add several pieces of information to a database and also display that information in an edit mode. The problem is, when I use the page for edit, not all of the...
1
by: Griff | last post by:
Hi! I'm using JavaScript on an HTML page for form validation. I have a form value (theForm.ProdType1) which represents a picklist from which users may make multiple selections; essentially,...
4
by: Bass Pro | last post by:
Hi, I am creating textbox, radiobuttonlist and checkboxlist dynamically depending on data from a table. It is a questionnaire. I add the control on a Panel control during the 1st load_page event....
10
by: ads | last post by:
hi, after binding the dropdownlist to a datasource, ive experience this error "Cannot have multiple items selected in a dropdownlist" after using the code:...
5
by: carrajo | last post by:
Hey, I'm trying to duplicate the following: Select List 1 --- Apple Orange Banana
3
by: imrantbd | last post by:
This is my first problem.Please help me. I have the following code: <head> <script language="JavaScript"> function addSrcToDestList() { destList1 = window.document.forms.destList; srcList...
5
by: Max | last post by:
Is there any way to set a select-multiple type <select multiple="multiple"with multiple selected options in scripting? Any idea about this is appreciative.
58
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also...
4
pradeepjain
by: pradeepjain | last post by:
i had posted the same code in javascript area !! this i am posting bcos of different problem. as you can see in ma code there is a dropdown called sort ...its sorts the result by the value he...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.