admin管理员组

文章数量:1426223

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' value. How do I get the value of all of the items in that listbox?

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' value. How do I get the value of all of the items in that listbox?

Share Improve this question asked Nov 15, 2010 at 9:32 DreamWaveDreamWave 1,9403 gold badges28 silver badges63 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

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.

本文标签: phpGet all multiple listbox valuesStack Overflow