Connecting Tech Pros Worldwide Forums | Help | Site Map

Retrieve Droplist data for form

Newbie
 
Join Date: Mar 2009
Location: Baltimore, MD USA
Posts: 8
#1: Mar 5 '09
I am uncertain of the exact terminology and I am extremely hopeful that some will be able to assist me. 8)

Below is a sample of the code - what I need is to collect the results of the selection.
In this case a user selects a city/cities (in Texas) - This will be added to their favorites list.

Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <title>Select Cities</title>
  3. <script language="javascript">
  4. function addOption(selectbox,text,value )
  5. {
  6.     var optn = document.createElement("OPTION");
  7.     optn.text = text;
  8.     optn.value = value;
  9.     selectbox.options.add(optn);
  10. }
  11. function removeOption(listbox,i)
  12. {
  13.     listbox.remove(i);
  14. }
  15. function addOption_list(){
  16.  for(i=document.drop_list.Category.options.length-1;i>=0;i--)    {
  17.   var Category=document.drop_list.Category;
  18.   if(document.drop_list.Category[i].selected){
  19.     addOption(document.drop_list.SubCat, document.drop_list.Category[i].text, document.drop_list.Category[i].value);
  20.     removeOption(Category,i);
  21.     }
  22.   }
  23. }
  24. function remOption(listbox,text,value )
  25. {
  26.     var optn = document.createElement("OPTION");
  27.     optn.text = text;
  28.     optn.value = value;
  29.     listbox.options.add(optn);
  30. }
  31.  
  32. function remove2Option(selectbox,i)
  33. {
  34.     selectbox.remove(i);
  35. }
  36. function remOption_list(){
  37.  for(i=document.drop_list.SubCat.options.length-1;i>=0;i--)    {
  38.   var SubCat=document.drop_list.SubCat;
  39.   if(document.drop_list.SubCat[i].selected){
  40.     remOption(document.drop_list.Category, document.drop_list.SubCat[i].text, document.drop_list.SubCat[i].value);
  41.     remove2Option(SubCat,i);
  42.     }
  43.   }
  44. }
  45. </script>
  46. </head>
  47. <body>
  48. <form name="drop_list" action="results.php" method="post">
  49. <table width="200" cellspacing="0" cellpadding="5" border="0">
  50.  <tr>  
  51.   <td valign="top" width="90">
  52.   <select name="Category" multiple="multiple" size="10">
  53.    <option value="Amarillo">Amarillo</option>
  54.    <option value="Austin">Austin</option>
  55.    <option value="Dallas">Dallas</option>
  56.    <option value="Galveston">Galveston</option>
  57.    <option value="Houston">Houston</option>
  58.    <option value="Irving">Irving</option>
  59.    <option value="Killeen">Killeen</option>
  60.    <option value="Laredo">Laredo</option>
  61.    <option value="Lubbock">Lubbock</option>
  62.    <option value="Plano">Plano</option>
  63.    <option value="San Antonio">San Antonio</option>
  64.    <option value="Waco">Waco</option>
  65.   </select>
  66.   </td><td valign="middle" width="20">
  67.   <input onclick="addOption_list()" ;="" value="ADD &raquo;" type="button"><br />
  68.   <input onclick="remOption_list()" ;="" value="&laquo; REMOVE" type="button"></td>
  69.   <td valign="top" width="90">
  70.   <select name="SubCat" multiple="multiple" size="10">
  71.   </select></td>
  72.  </tr>
  73. </table><br />
  74. <input type="submit" value="Select">
  75. </form>
  76. </body>


The results are supposed to be sent to another page - However, they are not able to be retrieved??
results.php => should show the information, correct?

Expand|Select|Wrap|Line Numbers
  1. <table cellspacing="2" cellpadding="0" width="100%" border="0">
  2.  <tr>
  3.   <td><? echo $SubCat; ?></td>
  4.  </tr>
  5. </table>
The information will eventually be inserted into a database. I am just trying to determine how I gather the users selection?

Thank in advance...

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#2: Mar 5 '09

re: Retrieve Droplist data for form


Quote:

Originally Posted by phillx View Post

The results are supposed to be sent to another page - However, they are not able to be retrieved??
results.php => should show the information, correct?

I think it's due to the fact, that the second select box has no option selected to be submitted by the form (it's no use to submit an unselected option, is it?).
Newbie
 
Join Date: Mar 2009
Location: Baltimore, MD USA
Posts: 8
#3: Mar 5 '09

re: Retrieve Droplist data for form


I agree - how do I go about making certain that this is accomplished?

I am able to have the items selected and moved to the other listbox...
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#4: Mar 6 '09

re: Retrieve Droplist data for form


after you move the user's selection, you have to select them again in the second box (you may do that just before sending).
Newbie
 
Join Date: Mar 2009
Location: Baltimore, MD USA
Posts: 8
#5: Mar 6 '09

re: Retrieve Droplist data for form


Dormilich -

Thank you for the reply.

I completely understand that - the problem or confussion that I am having is how do I go about reselecting the transferred data?

Can you provide me with a possible solution?

Thanx
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#6: Mar 6 '09

re: Retrieve Droplist data for form


basic idea (untested):
Expand|Select|Wrap|Line Numbers
  1. function addOption(selectbox,text,value )
  2. {
  3.     var optn = document.createElement("option");
  4.     optn.text = text;
  5.     optn.value = value;
  6.     optn.selected = "selected";
  7.     selectbox.options.add(optn);
  8. }
Newbie
 
Join Date: Mar 2009
Location: Baltimore, MD USA
Posts: 8
#7: Mar 6 '09

re: Retrieve Droplist data for form


Dormilich -

Awesome!

However, I just tested the above and it only partially works. :-(

This will highlight (select) each item.

Although, only the last submission to be transferred into the second listbox is actually selected (sent on to the following page) ??

hmmm - any suggestions on how to select all items? I know it has been done on other sites - I am just not certain how to go about accomplishing it?

I really appreciate your (and anyone elses) assistance!
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Mar 7 '09

re: Retrieve Droplist data for form


The correct add method has two arguments, but IE uses one, so for cross-browser support, try this:
Expand|Select|Wrap|Line Numbers
  1.     optn.selected = true;
  2.     try {
  3.       selectbox.options.add(optn,null);
  4.     } catch {
  5.       selectbox.options.add(optn);
  6.     }
  7.  
In your PHP code, are you setting the $subCat variable properly to get the posted values?
Newbie
 
Join Date: Mar 2009
Location: Baltimore, MD USA
Posts: 8
#9: Mar 8 '09

re: Retrieve Droplist data for form


Acoder -
Unfortunately - that Code produced several javascript errors?!

Yes, I do have the the code getting the $subCat otherwise I would have no results? I am a bit confused by that. Is there another way of retrieving data?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#10: Mar 9 '09

re: Retrieve Droplist data for form


Are you sure you replaced the code in addOption()?

You can change the name of the options to subCat[] to use it as an array in PHP.
Reply

Tags
form, html, javascript, mysql, php