0

Problems:

The save works, but is only storing one of the options in the database after save.

Options not showing as selected after save.

The select fields:

<select id="exclude_page_from_cookies" name="exclude_page_from_cookies[]" multiple="multiple">
        <?php

        $pages = get_pages();
        foreach ( $pages as $page ) {
            $title = $page->post_title;
            $id    = $page->id;
            ?>

            <option id="<?php echo $id; ?>" value="<?php echo $title ?>" <?php selected( $title ); ?> >
                <?php echo $title;?>
            </option>
        <?php
        }
        ?>
    </select>

The save function:

if ( isset( $_POST['exclude_page_from_cookies'] ) ) {
    foreach( $_POST['exclude_page_from_cookies'] as $exclude_page ) {
         echo $exclude_page;
         update_option( 'exclude_page_from_cookies', $exclude_page ) ;
    }
}
Dsmart
  • 884
  • 6
  • 8
  • Related (if not duplicate): http://stackoverflow.com/questions/2407284/how-to-get-multiple-selected-values-of-select-box-in-php – ComFreek May 17 '14 at 17:35
  • thanks but that doesn't have anything about update_option() or any save functions. – Dsmart May 17 '14 at 17:40
  • 1
    `$_POST['exclude_page_from_cookies']` will be an array if using `exclude_page_from_cookies[]`. Just loop over it and call `update_option()` every time (assuming `update_option()` does not overwrite previous values - consult the approriate documentation of the function.) – ComFreek May 17 '14 at 17:43

1 Answers1

1

I'm assuming selected() is a wordpress function?

I guess your line should look like this:

 <option <?php selected( $title ); ?> value="<?php echo $title ?>">

(with selected outside of value="")

EDIT

as per @comfreak:

foreach($_POST['exclude_page_from_cookies'] as $exclude_page ){
 update_option('exclude_page_from_cookies',$exclude_page);
}
andrew
  • 9,313
  • 7
  • 30
  • 61