Get value from HTML multi select

Discussion forum for Extension Writers regarding Extension Development.
User avatar
danieltj
Infrastructure Team Member
Infrastructure Team Member
Posts: 518
Joined: Thu May 03, 2018 9:32 pm
Location: United Kingdom
Name: Daniel James

Get value from HTML multi select

Post by danieltj »

I have a multi-select element and I'm trying to capture the values in it but it's not working and I've got no idea why the value isn't getting passed through to the $request variable. I'm sure I'm missing something very simple.

The HTML from my template:

Code: Select all

<select name="group_membership[]" id="group_membership" multiple="multiple" size="10">
	{% for group in GROUPS %}
		<option value="{{ group.id }}">{{ group.name }}</option>
	{% endfor %}
</select>
This is how I'm trying to capture the values in my controller.

Code: Select all

$memberships = $request->variable( 'group_membership', '' );
var_dump( $memberships ); die();
This is what the var_dump() is outputting.

Code: Select all

/Users/danieltj/Local Sites/phpbb/app/public/ext/danieltj/extension/controller/acp.php:161:string '' (length=0)
Any ideas what I'm doing wrong?
💷 Purchase the Awesome Payments extension today!
Monetise your forum with one off payments and subscriptions.

Need a premium extension created? Send me a PM.
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 28851
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier

Re: Get value from HTML multi select

Post by Paul »

it is a array of string (Or array of ints by the look of the field name), so you second parameter of variable should be a array of string as well.
User avatar
danieltj
Infrastructure Team Member
Infrastructure Team Member
Posts: 518
Joined: Thu May 03, 2018 9:32 pm
Location: United Kingdom
Name: Daniel James

Re: Get value from HTML multi select

Post by danieltj »

Paul wrote: Wed Jul 24, 2024 10:41 am it is a array of string (Or array of ints by the look of the field name), so you second parameter of variable should be a array of string as well.

Code: Select all

$request->variable( 'group_membership', [ 0 ] )
That fixed it, thank you.
💷 Purchase the Awesome Payments extension today!
Monetise your forum with one off payments and subscriptions.

Need a premium extension created? Send me a PM.

Return to “Extension Writers Discussion”