Forum Moderators: coopster

Message Too Old, No Replies

Question: on mailing checkbox values from a form

         

fetlock

10:40 pm on Dec 29, 2003 (gmt 0)

10+ Year Member



I'm mailing the contents of a form. That's all working fine.

However, the checkbox values don't behave as I expected (I'm used to asp, and getting a comma seperated list).

After reading posts here and elsewhere, this is what I have. It's so close, and gets the job done, but I suspect it's not quite how it should be. I hoped to loop through the array and concatenate the results into one string, so that I could add just that one string to my message string.

Any one spot what I could do differently?

From the form:
<input type="checkbox" name="someitems[]" value="First Item"> First Item
<BR><input type="checkbox" name="someitems[]" value="Another Item"> Another Item
<BR><input type="checkbox" name="someitems[]" value="Someother Item"> Someother Item
...etc

From the mail sending page:

$msgA = "Some message.\n";
$msgB = "\nItems Selected:\n " . $someitems[0] . ", " . $someitems[1] . ", " . $someitems[2] . ", " . $someitems[3] . ", " . $someitems[4] . ", " . $someitems[5] . ", " . $someitems[6] . ", " . $someitems[7] . ", " . $someitems[8] . ", " . $someitems[9];
$msg = $msgA . $msgB;
mail($to, $subject, $msg, "From: asdf@somedomain.com") ;
Crude, but it works.

I tried looping and could echo the values ok, but didn't find the way to save those values to a variable that I could add to the message variable.

for($i=0; $i<count($someitems); $i++) echo $someitems[$i];

Thanks.

jatar_k

10:48 pm on Dec 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld fetlock,

what about something like this?

$msgB = "\nItems Selected:\n ";
for($i=0; $i<count($someitems); $i++) $msgB .= $someitems[$i] . ",";

fetlock

12:47 am on Dec 30, 2003 (gmt 0)

10+ Year Member



Hurrah!

That does it! Thank you!

Now I know :)

jatar_k

1:33 am on Dec 30, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



just remember that it will leave a trailing comma at the end which you may want to remove. Maybe something like this after the loop finishes.

$msgB = substr($msgB,0,strlen($msgB)-1);

fetlock

2:16 pm on Dec 30, 2003 (gmt 0)

10+ Year Member



Good idea, Thanks!

I made it -2 since I added a space after the comma.

dcrombie

12:00 pm on Jan 4, 2004 (gmt 0)



$msg = "Items: " . implode (", ", $someitems);

fetlock

1:51 pm on Jan 4, 2004 (gmt 0)

10+ Year Member



Ah, so that's what implode means. I kept coming across it, but never found a meaning for it.
Thanks.