Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

The dreaded Multiselect form on input_form.html

Quote Reply
The dreaded Multiselect form on input_form.html
I've been trying to get multiple select forms working on my input_form template for ages. Just when I thought I had them beat I find another problem.

First I found that "ENUM" fields would not work with multiple select forms, and got things working using type "VARCHAR" or "TEXT". All seems well...

Next, I find the the multiple select form is not showing values on the modify.html template. Changing to "contains" instead of "eq" in the comparison displays selections on the modify page properly using the code below...

<select id="features" name="features" multiple="multiple" size=5>
<option value="None" <%if features contains 'None'%>selected<%endif%>>------None------</option>
<option value="Option1" <%if features contains 'Option1'%>selected<%endif%>>Option1</option>
<option value="Option2" <%if features contains 'Option2'%>selected<%endif%>>Option2</option>
<option value="Option3" <%if features contains 'Option3'%>selected<%endif%>>Option3</option>
</select>

The problem is, if there is an error made by users when adding or modifying a link the selections on the multiselect forms are lost. All except for the the values to the right, or rather the last one selected.

From the docs: "contains will be true if the variable contains the right-hand side."

Why would the code work on a the modify template, but not after an error???

Is there a way to fix this, or do I need give up and get started changing over to checkboxes???

Thanks
Quote Reply
Re: [Jonze] The dreaded Multiselect form on input_form.html In reply to
I had once tried working with multi-select, however after tons of tries, I kinda realized that for now checkboxes options seems much easier to setup.

Vishal
-------------------------------------------------------
Quote Reply
Re: [Jonze] The dreaded Multiselect form on input_form.html In reply to
If a value allows multiple values to be selected, then when it gets submitted, it turns into an array in the template. That means you have to loop through the array and check each value:
Code:
<option value="Option1"<%loop features%><%if loop_value eq $Option1%> selected="selected"<%lastloop%><%endif%><%endloop%>>Option1</option>

Adrian

Last edited by:

brewt: Mar 28, 2007, 11:07 AM
Quote Reply
Re: [brewt] The dreaded Multiselect form on input_form.html In reply to
Thanks a lot for the help!

I just tried that code, but using that loop I get the following error....

"Error: Unmatched endif/endifnot/endunless tagError: endloop found outside of loop"

I'm still hoping to get this working, although haven't been able to work out a successful loop statement.

Anyone know how to fix that loop???
Quote Reply
Re: [Jonze] The dreaded Multiselect form on input_form.html In reply to
Hi,

I think there was an extra <%endloop%> in there =) Try this:

<option value="Option1"<%loop features%><%if loop_value eq Option1%> selected="selected"<%endif%><%endloop%>>Option1</option>

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] The dreaded Multiselect form on input_form.html In reply to
Hey Andy,

That's one thing I've tried, and it doesn't seem work. =(

I have that loop loaded into my templates right now. Although there are no errors, there are also no selected values displayed in the form.

Still scratching my head on this one...

Thanks all the same bro!
Quote Reply
Re: [Jonze] The dreaded Multiselect form on input_form.html In reply to
Hi,

You could try this global:

Code:
sub {
my $column = $_[0];
my $selected = $_[1];
my $table = $DB->table("Users");
my %hash;
my $i = 0;
my $names = $table->form_names->{$column};
my $vals = sort $table->form_values->{$column};
for my $val (@$vals) {
$hash{$val} = $names->[$i++];
}
my $html = $DB->html($table, $IN);
my $form = $html->select({
name => "$column",
values => \%hash,
multiple => 1,
value => $selected });
return $form;
}

<%global_name('features',$features)%>

I use that most of the time for really annoying (i.e long =)) SELECT box lists <G>

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] The dreaded Multiselect form on input_form.html In reply to
Actually found that global in the last night =)

Played with it a bit, but couldn't seem to get it doing what I need.

Again, thanks for the effort Andy!
Quote Reply
Re: [Jonze] The dreaded Multiselect form on input_form.html In reply to
Try the revised version.

Adrian
Quote Reply
Re: [brewt] The dreaded Multiselect form on input_form.html In reply to
Just tried the revised version, and it still doesn't return selections. No errors, but only produces a non selected multiselect form.

Any other suggestions???

Last edited by:

Jonze: Mar 28, 2007, 2:49 PM
Quote Reply
Re: [Andy] The dreaded Multiselect form on input_form.html In reply to
Andy,

Thanks a lot for that very useful global... I had something wrong which caused it not to work. Although it doesn't return selected values for the multiselect field when a user has an error.

I actually found another global in the forums you made to save an array...

sub {
my $tmp = $_[0];
my @array = split /\n/, $tmp;
my @new_array; foreach (@array) {
chomp;
my $tmp_hash;
$tmp_hash->{value} = $_;
push @new_array, $tmp_hash; }
return { features_loop => \@new_array }
}

<%array($features)%>

<option value="None"<%loop features_loop%><%if value eq None%> selected="selected"<%endif%><%endloop%>>------None------</option>
<option value="Option1"<%loop features_loop%><%if value eq Option1%> selected="selected"<%endif%><%endloop%>>Option1</option>
<option value="Option2"<%loop features_loop%><%if value eq Option2%> selected="selected"<%endif%><%endloop%>>Option2</option>

again this works as well but if there an error by users the selected values are lost. Tried many different things including "contains" instead of "eq" with no luck.

Why upon error do these methods only return "true" for the last value in the db stored like this...

None
Option1
Option2
Option3

If anyone knows how to read all values stored after an error I'd love to know.. This is going to drive me nut!

Thanks guys!