Old 10-05-2007, 11:06   #1 (permalink)
mx
fucksocks™
 
mx's Avatar
 
Join Date: Jun 2005
Location: in the boosh
Posts: 1,578
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
Old 10-05-2007, 19:17   #2 (permalink)
mx
fucksocks™
 
mx's Avatar
 
Join Date: Jun 2005
Location: in the boosh
Posts: 1,578
anyone? anyone? fry? fry?
__________________
  Reply With Quote
Old 11-05-2007, 09:41   #3 (permalink)
mx
fucksocks™
 
mx's Avatar
 
Join Date: Jun 2005
Location: in the boosh
Posts: 1,578
no-one?
Oh well. I've fixed it anyway. needed to define the array as a global variable in the function. So there.
__________________
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8