Page 1 of 1

Displaying Check Box Results in Email

Posted: Sat Dec 12, 2009 11:23 pm
by Thursday
I am working on a newsletter script for my website. The users register for one or more newsletters offered by my site and the data is sent to a process.php page which then sends out a confirmation email. The problem is it is only displaying results for one of the newsletters rather then all.

Please review the information below. If all the information is correct then click on the confirmation link to confirm your subscription.

You have subscribed to the following newsletters:
following that part of the email it should list all of the letters you subscribed to, but it only lists one.

my code for that looks like this.

Code: Select all

$letter=$_POST['letter'];
foreach ($letter as $letters)
{
echo "$letters </br>";
}
how can i get it to return all of the lists to the email instead of just one?

Re: Displaying Check Box Results in Email

Posted: Sun Dec 13, 2009 1:08 pm
by galfrid
I'm playing with this concept as we speak - I'm noticing that if you say

Code: Select all

print_r($letter);
BEFORE your foreach, no matter how many checkboxes are selected, it will only output the last one selected.
I'd thought to suggest you use a more verbose foreach:

Code: Select all

$letters = $_POST['letters'];
foreach($letters as $key => $letter)
{
echo "$letter <br>";
}
But the problem seems to be in the actual assignment of value to your original $_POST['letters'] - it only picks the last one. I will try to post a solution (I'm working on a similar issue myself, I'd love to know how to make this work) if I can get one.
The only work-around I might suggest is giving each of your checkboxes a different name value and array them yourself, but that seems like more work than should be necessary.

Re: Displaying Check Box Results in Email

Posted: Sun Dec 13, 2009 1:14 pm
by Thursday
I got a reply on another message board, that solved the problem. I'll share it here for you as well.

Code: Select all

$emailbody = "Selected values: "  . implode("," , $_POST["letter"]);

Re: Displaying Check Box Results in Email

Posted: Sun Dec 13, 2009 1:17 pm
by galfrid
Oh awesome! I still think it's weird that the checkbox family isn't really considered an array, but your suggestion does exactly what I was hoping to achieve too - Thanks for sharing the knowledge, it's how we all get a little smarter ^_^