Forum Moderators: coopster

Message Too Old, No Replies

multiple form select problem

output to formatted html email

         

LadynRed

10:35 pm on Mar 22, 2004 (gmt 0)

10+ Year Member



Well, you guys got me this far, now I'm stuck.

I now have my form output nicely formatted and going to email. However, I can't figure out what I'm doing wrong with the form's multiple select boxes. There are 5 fields on the form that allow multiples.

Here's how I have one setup on the html form:


<select name="emp_badge_sel[]" size="6" multiple="multiple" id="emp_badge_sel">
<option>Bldg 1</option>
<option>Bldg 2</option>
<option>Bldg 3</option>
<option>24 x 7 (COO App)</option>
<option>Weekend (COO App)</option>
<option>Conference Rms</option>
</select>

And here is the portion of the script:

<tr>
<td width=13>Badges:".$_POST['emp_badge_sel']."</td>
</tr>

When that goes thru my script and comes out in the formatted email, what I get is Badges:Array

Ok, so I changed the script to this: <td width=13>Badges:"$my_array[.$POST[emp_badge_sel']]."</td>

What I get then is a "parse error, unexpected T_VARIABLE" on the first line where I put $my_array

What am I missing here?

TIA :)

Birdman

11:06 pm on Mar 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to loop through the array and print the results for each array index. Something like:

$badges = "";
$count = count($_POST["emp_badge_sel"];
for( $i = 0; $i < $count; $i++ ){
$badges .= $_POST["emp_badge_sel"][$i]"\r\n";
}

Now $badges is a string containing the selected values.

LadynRed

4:50 pm on Mar 23, 2004 (gmt 0)

10+ Year Member



Ok, but I have 5 multi-select fields. I'd have to add that script for EACH field.

Do I put that looping code at the end or right in there where the $my_array[.$_post['select name']] is?

Sorry, I'm a complete newcomer to PHP and trying to pick it up on-the-fly. I do the web sites, wear ALL the hats, and now there are 3 more people who have submitted form requests. Picking up a new language on the run isn't my ideal, but its what I'm stuck with for now.

Thanks!

Birdman

5:15 pm on Mar 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since you need to reuse the code, you should put it in a function like this:

function multiSelect($element){//$element will be the post var that you pass in
$badges = "";
$count = count($_POST[$element]);
for( $i = 0; $i < $count; $i++ ){
$badges .= $_POST[$element][$i]."-";
}
return $badges;
}

You can put that code near the top of your script and to get your result, do something like this where you want the results printed:

<?=multiSelect($_POST["select_1"])?>
<?=multiSelect($_POST["select_2"])?>
...

That's using shorthand. If it gives you an error, try:

<?php print multiSelect($_POST["select_1"]);?>
<?php print multiSelect($_POST["select_2"]);?>
...

LadynRed

5:21 pm on Mar 23, 2004 (gmt 0)

10+ Year Member



Thanks Birdman. This is getting deeper than I expected, but its not wasted. I'm certain I'll have to do this again. I'm definitely signing up for a class in PHP in May!

The output from my form/script is going into an html-formatted email. Do I still use Print?

This was SO much easier in my Cold Fusion days ;)

Birdman

9:35 pm on Mar 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, use the above techniques to print the results to an email or to a webpage. Give it a test run and let us know how you fare.

Birdman