Displaying Check Box Results in Email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Thursday
Forum Newbie
Posts: 2
Joined: Sat Dec 12, 2009 11:19 pm

Displaying Check Box Results in Email

Post 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?
galfrid
Forum Newbie
Posts: 7
Joined: Sun Dec 13, 2009 11:52 am

Re: Displaying Check Box Results in Email

Post 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.
Thursday
Forum Newbie
Posts: 2
Joined: Sat Dec 12, 2009 11:19 pm

Re: Displaying Check Box Results in Email

Post 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"]);
galfrid
Forum Newbie
Posts: 7
Joined: Sun Dec 13, 2009 11:52 am

Re: Displaying Check Box Results in Email

Post 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 ^_^
Post Reply