0

I've been putting together an advanced post notification system for wordpress custom posts, based on Carlo Daniele's post here: http://www.smashingmagazine.com/2015/05/building-wordpress-notification-system/

I'm trying to modify it so that user can select multiple States/Areas, where I'm running into trouble is posting the selected states to the database.

    $smashing_notification_states = array( '- Dublin City Centre -' => '- Dublin City Centre -', '- North Dublin City -' => '- North Dublin City -', 
'- South Dublin City -' => '- South Dublin City -', 
'- North Co. Dublin -' => '- North Co. Dublin -', 
'- South Co. Dublin -' => '- South Co. Dublin -', 
'- West Co. Dublin -' => '- West Co. Dublin -', );

Select Area: `

        <td>
            <label for="state">
                <select  multiple="multiple"  name="state">
                    <option value="" <?php selected( get_user_meta( $user->ID, 'state', true ), "" ); ?>>Select</option>
                    <?php foreach ($smashing_notification_states as $key => $value) { ?>
                        <option value="<?php echo $key; ?>" <?php selected( esc_attr( get_user_meta( $user->ID, 'state', true ) ), $key ); ?>><?php echo $value; ?></option>
                    <?php } ?>
                </select>
                <?php _e( 'Select area', 'smashing' ); ?>
            </label>
        </td>
    </tr>`

if( isset($_POST['state']) ) update_user_meta( $user_id, 'state', sanitize_text_field( $_POST['state'] ) );

I've pushed and pulled at it but to no avail, tried. Last thing I tried was changing <select multiple="multiple" name="state"> to <select multiple="multiple" name="state[]">

This posted as "Array"

Any help would be great as always!

Waterbarry
  • 55
  • 6

1 Answers1

0

Using <select multiple="multiple" name="state[]"> works because you are getting "Array" as a value. The problem is that a database doesn't understand PHP data types, so you need to convert it into something it will understand... a quick search:

Insert array into MySQL database with PHP

Or you can opt to serialize the data and insert it that way as well:

http://php.net/manual/fr/function.serialize.php

Community
  • 1
  • 1
Daniel C
  • 2,105
  • 12
  • 18