473,378 Members | 1,377 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,378 software developers and data experts.

Processing Multiple Select Lists

2
Hi Y'all,

I am pretty new to js and am hitting a few snags. I need to process a multiple select list and pass it onto a PHP script.

I can only get the first value in the JS and have no idea how to grab the rest and successfully pass the array onto my php script.

No books, foums, examples, tutorials or anything I have spent most of my recent waking hours reading and downtime dreaming about appear to cover this and I am starting to think that either nobody knows how to do this.

Ultimately I would like to make it a generic function so that it can be reused, there seem to be lots of people seeking a solution to this.

This is where I am at with it:

Example Form:
Expand|Select|Wrap|Line Numbers
  1. <select name="countryarray[]" multiple="multiple">
  2. <option value="AF">Afghanistan</option>
  3. <option value="AL">Albania</option>
  4. <option value="AS">American Samoa</option>
  5. <option value="AD">Andorra</option>
  6. .....</select>
JS Function:
Expand|Select|Wrap|Line Numbers
  1.     function getformvalues (fobj, valfunc){
  2.  
  3.  
  4.  
  5.         var str = "";
  6.  
  7.         aok = true;
  8.  
  9.         var val;
  10.  
  11.  
  12.  
  13.         //Run through a list of all objects contained within the form.
  14.  
  15.         for(var i = 0; i < fobj.elements.length; i++){
  16.  
  17.             if(valfunc) {
  18.  
  19.                 if (aok == true){
  20.  
  21.                     val = valfunc (fobj.elements[i].value,fobj.elements[i].name); 
  22.  
  23.                     if (val == false){
  24.  
  25.                         aok = false;
  26.  
  27.                     }
  28.  
  29.                 }
  30.  
  31.             }
  32.  
  33.                     if (fobj.elements[i].type == "checkbox") {
  34.                            if (fobj.elements[i].checked) {
  35.                               str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
  36.                            } else {
  37.                               str += fobj.elements[i].name + "=&";
  38.                            } 
  39.  
  40.             } else if (fobj.elements[i].tagName == "SELECT") {
  41.                         var sel = fobj.elements[i];
  42.                 if (fobj.elements[i].id == "countryarray") {
  43.                 var numopts = document.forms[0].elements['countryarray[]'].length
  44.  
  45.                     for(var q=0;q<=numopts;q++){
  46.                             // WHAT NOW?    
  47.                     }
  48.                 } else {
  49.                         str += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
  50.                 }
  51.  
  52.                     } else {
  53.  
  54.                 str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
  55.             }
  56.  
  57.         }
  58.  
  59.         //Then return the string values.
  60.  
  61.         return str;
  62.  
  63.     }
  64.  
  65.  
Thanks for any assistance / pointers

Bernard
Mar 26 '07 #1
2 2253
acoder
16,027 Expert Mod 8TB
Welcome to TSDN.

You don't need to go through all that effort.

See this page.
Mar 28 '07 #2
dinkle
2
Thanks for the reply.

I think that you mis understood what I mean. My PHP coding is ok and I know how to receive and process the array.

The app I am working on uses ajax for form posting and page loading. So I need a js handler to pre process the multiple select before it is passed on to PHP.

I know it is more work to achieve the same end, but I need to do it this way as the app works like it :(

Thanks
Mar 28 '07 #3

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

Similar topics

1
by: Danny Anderson | last post by:
Hola, PHP folk! I have a php page that contains a self-processing form. The form holds search results. The search terms originally came from the previous page, but the user can repeatedly...
3
by: Henri Schomaecker | last post by:
Hi folks, does php 5 have a problem with html select lists which allow to choose multiple items? Whatever I tried I just can't get another or more than the last item selected in the list....
7
by: Felix Natter | last post by:
hi, I have a php-Script which gets passed a Boat-ID and this is used to mark an element in a <select> as the default: <select name="boote" multiple="multiple" size="5"> <option...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
15
by: Jack | last post by:
Hi, I have a asp form where one element is a list box which lists four years starting from 2004. This list is drawn from a database table which has YearID and Year as two fields as shown below:...
1
by: abhishekhs | last post by:
Hi all I have more than one multiple select lists in a page. Something like this <tr> <td> <select NAME="StrainList" ID="StrainList" SIZE="5" multiple="multiple" style="width: 150px"> <?...
2
by: anchi.chen | last post by:
Hi People, Just wondering if any of you have ever come across any javascript examples that will allow one to drag and drop multiple items between lists? That is, users would be able to use the...
4
by: rn5a | last post by:
A Form has 2 select lists. The 1st one whose size is 5 (meaning 5 options are shown at any given time) allows multiple selection whereas the 2nd one allows only 1 option to be selected at a time. ...
5
by: bennever | last post by:
Hi, I plan to use drop down lists to populate team results and will use the standard country/state drop down lists as the base code. The question: how can I populate multiple (5) states from...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.