Get all option-values from a multi-select

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
highway_900
Forum Newbie
Posts: 2
Joined: Thu Sep 03, 2009 11:07 pm

Get all option-values from a multi-select

Post 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!!
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

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

Post 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?
sousousou
Forum Commoner
Posts: 29
Joined: Fri Aug 28, 2009 1:10 pm

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

Post by sousousou »

Left = all options
Right = selected

Left - Right = unselected ?

Or do I misinterpret the problem?
highway_900
Forum Newbie
Posts: 2
Joined: Thu Sep 03, 2009 11:07 pm

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

Post 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!!!
Post Reply