1

I want to create a PHP array of a select list. The list have dynamic options, the options are filled with the help of javascript. I want to get all the options on next page. So I want to create an array of options. Is there any another way to complete this stuff? Can anybody help me out? Thank you so much in advance.

<script type = "text/javascript">
 var val = "";
  function removeOptions(selectbox) {
val = selectbox.value;
for (var i = selectbox.options.length-1; i>=1; i--) {
    if (selectbox.options[i].selected) {
        addOption(document.form1.list2,val,val);
        selectbox.remove(i);
        document.form1.list1.focus();
    }
}
}

  function addOption(selectbox,optiontext,optionvalue ){
  var optn = document.createElement("OPTION");
  optn.text = optiontext;
  optn.value = optionvalue;
  selectbox.options.add(optn);
  }</script>

  //list1
<select name="list1" size="7" multiple="multiple" id="jumpMenu" onchange="removeOptions(this)" style="width:200px">
      <option>Choose Area...</option>
      <?php foreach($dbh->query($sql) as $row){
       $a=$row['name'];?>
        <option value="<?php echo $a?>"><?php echo $a?></option>
  <?php  }?>
   </select>
    //list2 
    <select name="list2" size="7" multiple="MULTIPLE" id="list2" style="width:170px">
     </select>
3
  • Can you show us your current code? Commented May 31, 2012 at 9:11
  • Not sure I understand your question, you want all the values of the select box to be sent to the next page? Why cant you just run your database query again on the next page or am I missing something? Commented May 31, 2012 at 9:46
  • @DavidBarker:You got my question. But the problem is on next page I didn't get the values of select box. That's why I want to create an array of options. So that, I can easily get the values on next page. Commented May 31, 2012 at 9:58

1 Answer 1

1

First: if You want to use multiple with select box, then this selectbox's name have to contain sharp brackets: name="list1[]" and name="list2[]" - this way a PHP will know that this is an array of values.

Second: learn jQuery - though it may seem hard in the beginning it will save You a lot of time and browser-compatibility problems in the future. And jQuery will save Your a*s many times in the future.

For Your purpose I would recommend not just using onchange events but implement additional 4 buttons between the two multiselectboxes that will move the selected options from one to another or all from one to another. This kind of multiselect looks like the one pictured below.

MultiSelectBox

By the first button >> all the options from 1. select are moved to the second one and vice versa with the 4th button <<. By the second button > only the selected option(s) is(are) moved the second select and vice versa with the third button <.

By this You only catch the click event on the buttons... That is really easy using jQuery...

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your valuable suggestion. I'll try to do the stuff as per your suggestion. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.