Multi select only saves 1 selected option. (The last one selected)

I'm new here, so if I have done anything incorrectly with regards to my question, please be "gentle" and perhaps offer guidance as to where I've gone wrong.

As per the title, I am trying to add a "multi select" field into an extension in Joomla, where options are selected in the item view in the administrator panel. Everything seems to be displaying correctly, but when I save the item, the only saved option out of the selected ones is the last one selected.

Here is the code I have used in the item view.

<?php if (count($this->multiselects) > 0): ?>
                    <?php foreach ($this->multiselects as $field): ?>
                        <li><label for="additional_multiselect[<?php echo $field->id ?>]"><?php echo $field->name ?></label>
                            <select multiple="multiple" name="additional_multiselect[<?php echo $field->id ?>]">
                                <option value=""></option>
                                <?php
                                $values = explode(PHP_EOL, $field->values);
                                foreach ($values as $v)
                                {
                                    ?>
                                    <option value="<?php echo $v ?>"<?php if (trim($v) == trim($this->multiselect_values[$field->id])) echo 'selected="selected"' ?>><?php echo $v ?></option>
                                    <?php
                                }
                                ?>
                            </select>
                        </li>
                        <?php endforeach; ?>
                    <?php endif; ?>

I have tried adding [] after the name, but this has not seemed to work. I also tried removing them from around the field->id part of the name and adding them at the end, but this does not seem to work.

Can anyone spot where I have gone wrong and point me in the right direction please?

Upvotes: 0

Views: 1773

Answers (2)

u_mulder
u_mulder

Reputation: 54831

Add another [] to your select name:

<select 
    multiple="multiple" 
    name="additional_multiselect[<?php echo $field->id ?>][]"
>

After form submit check your $_POST array:

print_r($_POST);

Upvotes: 1

Ghulam Ali
Ghulam Ali

Reputation: 1935

If you want multiple values you must use [] at the end of input name otherwise you will only receive the last value.

<select multiple="multiple" name="additional_multiselect[]">

After you have got the values you can use print_r($_POST); or print_r($_GET); (depending on your formt type) to verify you have received multiple inputs. Then when you save them make sure you are not overlaping each items but saving all of them. (You haven't showed your save code)

Upvotes: 1

Related Questions