View Single Post
Old 10-05-2007, 11:06   #1 (permalink)
mx
fucksocks™
 
mx's Avatar
 
Join Date: Jun 2005
Location: in the boosh
Posts: 1,622
Multiple selection box and PHP

I'm trying to get a multiple selection box to submit it's values to PHP, encode them so they can be submitted into a database, and then retrieved and have the options reselected in the selection box.

So far the submission, encoding and retrieval is working fine, however the function I've created to reselect the options doesn't seem to understand the array.

Any ideas?

PHP Code:
<?
// if from has been posted
if ($_POST) {

    
// Get array from select box
    
$typeArray = array();
    foreach (
$_POST['multi'] as $element) {
          
$typeArray[] = $element;
    }

    
// serialise array ready for submission to database
    
$typeCom serialize($typeArray);
    echo 
"<p>" $typeCom "</p>";
    
    
// Unserialise array ready for use
    
$typeNew unserialize($typeCom);

    
// Functio to reselect elements in selectbox based on array values
    
function retrieveType($type) {
        foreach (
$typeNew as $element) {
              if (
$element == $type) {return "selected" ;}
        }
    }
}
?>

<fieldset>
<legend>Multiple Item Select</legend>

<form action="index.php" method="post">

<select name="multi[]" multiple="multiple">
<option value="one" <? if($_POST) { echo $i retrieveType('one'); } ?>>one</option>
<option value="two" <? if($_POST) { echo $i retrieveType('two'); } ?>>two</option>
<option value="three" <? if($_POST) { echo $i retrieveType('three'); } ?>>three</option>
<option value="four" <? if($_POST) { echo $i retrieveType('four'); } ?>>four</option>
<option value="five" <? if($_POST) { echo $i retrieveType('five'); } ?>>five</option>
</select>

<input type="submit" value="submit" name="submit" />

</form>

</fieldset>

There's a live version here if that helps.

Cheers beers
__________________
  Reply With Quote