Page 1 of 1

Get all option-values from a multi-select

Posted: Thu Sep 03, 2009 11:23 pm
by highway_900
Hello Everybody!
I'm pretty new to php(second day) so please be kind. :/

I have two select boxes that I can move options between(javascript) I populate the right box
with options from the left then the goal is to press submit and send the options in the right box to a php script.

The problem I'm having is that the script "test.php" only receives the option elements that are selected(go figure)
Is there a way of using $_POST to get all the option values in the right hand box(not selected)?
An example....
The html file.

Code: Select all

<html>
<head></head>
<body>
 
<form action="test.php" method="POST">
<select name="OS[]" size="5" multiple>
<option>Windows</option>
<option>Linux</option>
</select>
<input type="submit" name="send">
</form>
 
</body>
</html>
The php file. This would echo only echo values that are selected.

Code: Select all

<?php
    foreach($_POST['OS'] as $key=>$value) {
        echo $key.' '.$value;
    }
?>
Thanks in advance!!

Re: Get all option-values from a multi-select

Posted: Fri Sep 04, 2009 5:28 am
by cpetercarter
The point of HTML forms is to send the options that are selected, not all the available options! But hang on, you say that you populate the RH box with options from the LH box. Does that mean that the selected options from the LH box = the option list in the RH box? If so, if you send the selected options from the LH box (eg as "OS_choice[]"), would that not represent the totality of options, selected and unselected, in the RH box?

Re: Get all option-values from a multi-select

Posted: Fri Sep 04, 2009 12:07 pm
by sousousou
Left = all options
Right = selected

Left - Right = unselected ?

Or do I misinterpret the problem?

Re: Get all option-values from a multi-select

Posted: Mon Sep 07, 2009 4:51 am
by highway_900
[SOLVED]
I have ended up using a hidden html input

Code: Select all

<form name="formA" method="post">
<select name="v[]">
<option value=1>one</option>
<option value=2>two</option>
</select>
</form>
This html input=hidden string will store the array of select values retrieved by a javascript.

Code: Select all

<input=hidden value=string>

Code: Select all

<script>
...
function selectToStr(selectArrray)
...
 
Then when I submit the form I...

Code: Select all

<input=submit onclick="document.formA.string = selectToStr(document.formA.element('v[]'));"/>
Now over in php I can...

Code: Select all

<?php
        $array = explode(",", $_POST['string']);
        foreach( $array as $key=>$value) {
        echo $key.' '.$value;
    }
?>
Woo hoo!!!