简体   繁体   English

动态选择列表和PHP数组

[英]Dynamic select list & PHP array

I want to create a PHP array of a select list. 我想创建一个选择列表的PHP数组。 The list have dynamic options, the options are filled with the help of javascript. 该列表具有动态选项,这些选项在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>

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. 首先:如果您要对选择框使用多个,则此选择框的名称必须包含尖括号: name="list1[]"name="list2[]" -这样,PHP将知道这是一个数组价值观。

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. 第二:学习jQuery-尽管一开始似乎很困难,但是它将在将来为您节省很多时间和浏览器兼容性问题。 And jQuery will save Your a*s many times in the future. jQuery将来会为您节省很多时间。

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. 为了您的目的,我不仅建议使用onchange事件,还建议在两个多选框之间实现额外的4个按钮,这些按钮会将选定的选项从一个移到另一个,或从一个移到另一个。 This kind of multiselect looks like the one pictured below. 这种多选看起来如下图所示。

多选框

By the first button >> all the options from 1. select are moved to the second one and vice versa with the 4th button << . 通过第一个按钮>> ,来自1. select的所有选项都移至第二个,反之亦然,第四个按钮<< By the second button > only the selected option(s) is(are) moved the second select and vice versa with the third button < . 通过第二按钮>仅所选择的选项(s)为(是)与所述第三按钮移动的第二选择,反之亦然<

By this You only catch the click event on the buttons... That is really easy using jQuery... 这样,您只捕获按钮上的click事件...使用jQuery确实很容易...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM