开发者

Get all multiple listbox values

I have a multiple selection listbox in which I insert items using javascript. At a certain point I need to get the values of all entries (both selected and unselected). I'm currently using this code:

<form method="post" action="?page=test" name="something">
<select name="thelist[]" id="selOriginalWindow" size="5" multiple="multiple">
</select>
<input type="button" value="Добави" onclick="openInNewWindow();" />
<input type="submit" value="Get" />
</form>

    <?
if ($_GET['page']=="test") {
    $thelist=$_POST['thelist'];
    var_dump($thelist);
}   
    ?>

Javascript inserts the values, but PHP only gets the selected items' va开发者_JAVA技巧lue. How do I get the value of all of the items in that listbox?


This did the trick:

function selectAll(selectBox,selectAll) {
    if (typeof selectBox == "string") {
        selectBox = document.getElementById(selectBox);
    }
    if (selectBox.type == "select-multiple") {
        for (var i = 0; i < selectBox.options.length; i++) {
            selectBox.options[i].selected = selectAll;
        }
    }
}
</script>
<form method="post" action="?page=test" name="something">
<select name="thelist[]" id="selOriginalWindow" size="5" multiple="multiple">
</select>
<input type="button" value="Добави" onclick="openInNewWindow();" />
<input type="submit" value="Get" onclick="selectAll('selOriginalWindow',true)" />
</form>

    <?
if ($_GET['page']=="test") {
    $thelist=$_POST['thelist'];
    var_dump($thelist);
}   
    ?>


The post variable will only contain those values you select, therefore if you want to use the same methodology to send ALL values you will need to add an additional field to your form which includes ALL the values. You can then read all values from this.

One way would be to use <input ='hidden' value='arrayofallvalues' name='allvalues'/>

So you could have the following:

<?
$select_values=array('value1', 'value2','value3');
?>

<form method="post" action="?page=test" name="something">
<select name="thelist[]" id="selOriginalWindow" size="5" multiple="multiple">
<?
for ( $i= 0; $i< count($select_values); $i++) {
    echo "<option value=".$select_values[$i].">".$select_values[$i]."</option>";
}

?>
</select>
<input ='hidden' value='".implode(",",$select_values)."' name='allvalues'/>
<input type="button" value="Добави" onclick="openInNewWindow();" />
<input type="submit" value="Get" />

</form>

    <?
if ($_GET['page']=="test") {
    $thelist=$_POST['thelist'];
    $all_select_values=explode(",",$_POST['allvalues']);
    var_dump($thelist);
}   
    ?>

What this will do is mean that every time the form submits, you can use explode() to create an array of the available values for the selct box.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜