Welcome Guest, Not a member yet? Register   Sign In
form_multiselect does not return array on $_POST
#1

[eluser]JoostV[/eluser]
Hi, I use a form_multiselect in one of my forms. However, on post, this multiselect does not retur an array, as expected. Instead, it return a string containing the last selected option.

Here's some sample code that I am using
Code:
$options['green'] = 'Green';
$options['orange'] = 'Orange';
$options['black'] = 'Black';
echo form_multiselect('multi', $options, array('orange', 'black'));

Say I select orange and black. Then, after post, $_POST['multi'] will return a string 'black'.

I am sure I am overlooking something obvious and it's driving me crazy! Can you help see what it is?
#2

[eluser]rogierb[/eluser]
Try it with []
Code:
echo form_multiselect('multi[]', $options, array('orange', 'black'));
#3

[eluser]JoostV[/eluser]
Works! Thanks, Rogier.
#4

[eluser]coffey[/eluser]
I found this form_multiselect() 'name' requirement a little annoying so I amended the function in an extension of form_helper.php I already had in place.

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

include_once( BASEPATH . '/helpers/form_helper'.EXT);

function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
    {
        if ( ! strpos($extra, 'multiple'))
        {
            $extra .= ' multiple="multiple"';
        }
        if ( ! strpos($name, '[]'))
        {
            $name .= '[]';
        }
        return form_dropdown($name, $options, $selected, $extra);
    }
?>

Just place this form_helper.php file in your application/helpers folder and it will override the system one. Hope this helps.




Theme © iAndrew 2016 - Forum software by © MyBB