Page 1 of 1

Use JHTML to create Multiple Select box

Posted: Mon Oct 06, 2008 1:33 pm
by babberoo
I have the following code in a custom component.

Code: Select all

<div class="formrow">
		<?php echo JHTML::_('select.genericlist',  $this->countries, 'countries_of_interest', 'class="inputbox" multiple size="8"', 'id', 'title'); ?>
	</div>
This populates the select list with countries from the database, which are available to be multiple selections. However, on submission, I'm only getting one value in my querystring and not an array of values. What am I missing?

Re: Use JHTML to create Multiple Select box

Posted: Mon Oct 06, 2008 2:27 pm
by babberoo
Sorted:

Code: Select all

<?php echo JHTML::_('select.genericlist',  $this->countries, 'countries_of_interest[]', 'class="inputbox" multiple="multiple" size="8"', 'id', 'title'); ?>
Note the [] at the end of the countries_of_interest.

I then implode my string to store in the database using:

$userdetails['countries_of_interest'] = implode(",", $userdetails['countries_of_interest']);

Re: Use JHTML to create Multiple Select box

Posted: Thu Sep 03, 2009 4:48 pm
by AmyStephen
Thank you!

Re: Use JHTML to create Multiple Select box

Posted: Wed Apr 13, 2011 4:07 pm
by soulman411
in an edit scenario, how do you set values for a multi-select box using JHTML?

Re: Use JHTML to create Multiple Select box

Posted: Sun Oct 30, 2011 2:20 pm
by vivec
for the ones coming past this topic, the answer is:

Code: Select all

explode(',',JRequest::getVar('country_ids'))
This converts the post-variable 'country_ids' into an array.
Your URL should be:

http://www.yourwebsite.com/index.php?op ... ,2,7,34,75

To quote the (now updated) example:

Code: Select all

<?php echo JHTML::_('select.genericlist',  $this->countries, 'countries_of_interest[]', 'class="inputbox" multiple="multiple" size="8"', 'id', 'title', explode(',',JRequest::getVar('country_ids'))); ?>