Forums

Select field with multiple selections

webmonkiee 02 Jan, 2008
I've seen other threads about how to get info from multiple checkboxes as an array, but what about a select field set to allow multiple selections?

I just get the last option selected.

I tried to do the following in my Email Template


foreach($_POST['field_name'] as $name){
  echo "$name\n";
}


but I get the following error in the email sent...

"Warning: Invalid argument supplied for foreach() in /hsphere/local/home/bndni/bndni.com/dev/components/com_chronocontact/chronocontact.php(317) : eval()'d code on line 8"

Any help or insight is greatly appreciated!
GreyHead 02 Jan, 2008
Hi webmonkiee,

The error message is because $_POST['field_name'] isn't an array (at least I think it is).

Please make sure that you have the select field with an array name like name='field_name[]'. You can check the result by turning debug on in the Form Manager General Tab and looking at the $_POST values that are displayed.

Bob
webmonkiee 02 Jan, 2008
you're right.

I was hoping there was another way around that. I'm filling the multiple select field based on the users choice from a prior select field. To do that i'm using javascript. When I target the multiple select field I get a js error if I use the field name "field_name[]" opposed to "field_name"

I'm sure there's some way around this. I'll look at my js.

Thanks! Happy new year.
webmonkiee 02 Jan, 2008
For anyone who might be trying to do the same thing I was there is a very simple answer which I'm ashamed to have missed. The name="field_name[]" works fine and for some reason I was targeting that with my js instead of adding a id="field_name"

So my select field ended up looking like this...


<select name="sub_products[]" id="sub_products" size="5" multiple="multiple">


and the js I called to target it looks like this...


onChange="fillSelectFromArray(this.form.sub_products, ((this.sel.....
GreyHead 02 Jan, 2008
Hi webmonkiee,

Excellent, good thought.

Bob
meadwench 15 Mar, 2008
I'm getting the same error, but it is happening because a particular checkbox is being left empty. I have 6 different checkbox fieldsets for types of lodging, and the user will likely only choose one.

How can I keep my form from throwing the error if a checkbox is left blank? I don't need this item to be required, this is just the way it will be filled out sometimes.

The code looks like this in the email template:

<tr><td><p><strong>Type B:</strong>  <?php
foreach ( $_POST['choose_b'] as $choose_b ) {
  echo "$choose_b, ";
}
?></p></td></tr>

<tr><td><p><strong>Type C:</strong>  <?php
foreach ( $_POST['choose_c'] as $choose_c ) {
  echo "$choose_c, ";
}
?></p></td></tr>


and like this in the form:
<tr><td><p><strong>Type B:</strong> (<input type="checkbox" name="choose_b[0]" 
value="Choose Type B" 
<?php 
if($_POST['choose_b'][0] == 'choose type b')echo' checked '; 
?> /> 
Choose this option or check one)<br> <em>
<input type="checkbox" name="choose_b[1]" value="Blue Wing" 
<?php 
if($_POST['choose_b'][1] == 'blue wing')echo' checked '; 
?> 
/>Blue Wing, 
<input type="checkbox" name="choose_b[2]" value="Green Wing" 
<?php 
if($_POST['choose_b'][2] == 'green wing')echo' checked '; 
?> 
/> Green Wing</em><br /> 
One-bedroom Apartment Suites. Located on the lowest level of the larger Manager's home and winter
office, these two suites have a living room (which has a double sofa bed) with fireplace, a full sized kitchen and a separate bath. Each has a walkout patio deck. Elevated a little from the lakefront and setback 124 ft from the lakefront, these suites have lovely long views of the lake and its gorgeous islands. The two suites can also be rented together as a two-bedroom duplex. The suites work well for people traveling alone, who do not want to be in an individual cabin alone. They also are a more economic option for couples or small families who want a full kitchen and fireplace.</p></td></tr>

<tr><td><p><strong>Type C:</strong> (<input type="checkbox" name="choose_c[0]" value="Choose Type C" <?php if($_POST['choose_c'][0] == 'choose type c')echo' checked '; ?> /> Choose this option or check one)<br> 
<input type="checkbox" name="choose_c[1]" value="Balsam" <?php if($_POST['choose_c'][1] == 'balsam')echo' checked '; ?> />Balsam, <input type="checkbox" name="choose_c[2]" value="Hemlock" <?php if($_POST['choose_c'][2] == 'hemlock')echo' checked '; ?> /> Hemlock, <input type="checkbox" name="choose_c[3]" value="Alder" <?php if($_POST['choose_c'][3] == 'alder')echo' checked '; ?> /> Alder and <input type="checkbox" name="choose_c[4]" value="Yellow Birch" <?php if($_POST['choose_c'][4] == 'yellow birch')echo' checked '; ?> /> Yellow Birch</em>
<br> 
All Type C units are individual log cabins along the quiet lakeshore of Teal Lake; with full kitchens and wood burning fireplaces. The full kitchens come with gas stoves, full refrigerator, pots and pans, utensils, plates, glasses, etc. Everything you should need to make a snack or a full meal. (if you need a bigger pot, come ask at the restaurant kitchen, we have a few to spare!). However each Type C has unique qualities, atmospheres, and layout. Only one has a screen porch, but all have comfortable seating outside with lovely lake views. Some have bigger living rooms while others have larger bedrooms.</p></td></tr>


Thanks!

Vicky Rowe
Edited to break up long line<br><br>Post edited by: GreyHead, at: 2008/03/15 16:11
GreyHead 15 Mar, 2008
Hi Vicky,

I'm not sure that I understand correctly but it might work to add a hidden field with a dummy value for each array. Not ideal but it would prevent the array being empty. Or you could add some PHP to test for an empty array.

Bob
meadwench 19 Mar, 2008
Hi Bob,

Do you think a space might be enough to have for the arrays to keep the fields from getting this error? I don't know what sort of PHP I would need to add to test for an empty array.

Vicky Rowe<br><br>Post edited by: meadwench, at: 2008/03/19 16:08
GreyHead 19 Mar, 2008
Hi Vicky,

The code is
<?php
if ( !empty($_POST['choose_b'] ) {
  // do something if the array isn't empty
}
?>
Bob<br><br>Post edited by: GreyHead, at: 2008/03/19 19:05
This topic is locked and no more replies can be posted.