Problem with Passing Checkbox Array Values to 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
jdb1981
Forum Newbie
Posts: 6
Joined: Mon Nov 17, 2008 1:55 pm

Problem with Passing Checkbox Array Values to Email

Post by jdb1981 »

I'm sure this is a simple fix, but PHP is not my strong suit, and I've googled my eyeballs out since last night searching for an answer to this, and nothing I've found works. There were several threads that came close on various forums, but nothing I can hang my hat on... I'm having an issue processing a form, specifically the values of checkboxes in said form. It seems to be working fine, but instead of sending an email with the checked values in the appropriate place, it just says: "Array." Some of what I've read suggests that I need to serialize the values, but everything I could find about how to do that is intended for passing them to a database, rather than an form email processing script, and didn't contain any information about unserializing them... I'm basically just confused, and I have a feeling I'm making a very simple mistake here.

Here's some snippets:

The Checkboxes themselves:

Code: Select all

 
Cake:<input type="checkbox" value="Cake" name="needs[]">
Rentals:<input type="checkbox" value="Rentals" name="needs[]">
DJ / Band:<input type="checkbox" value="DJBand" name="needs[]">
 
There's like 8 more, same format...

The code that's supposed to be passing them:

Code: Select all

 
<?php foreach($_POST['needs'] as $value) {
               $needs .= "$value\n";
        }      
?>
 
And the bit in the mailer script that gathers that value is:

Code: Select all

 
$needschecked = $_REQUEST["needs"];
 
Which then passes that "needschecked" value along with other values to the body of the message. I'm not sure where the error is occurring, only that it validates, sends the email, and arrives with just "Array" on the "Needs:" line.

Any help, even just a prod toward a link that might shed some light on this would be great... I don't know enough to know where I'm going wrong. Should I be using GET instead of POST? Is the captcha screwing it up without causing an error bad enough to prevent it from passing to the emailer? (I actually have some security doubts on the onsubmit function, which I didn't modify along with the rest of the code, since it was working fine and I don't want to cross-pollinate that problem with this one..)

Full code attached, (er, I thought I attached it, but I don't see it anywhere... I can post it in the body if need be, or is there something about the "upload attachment" box I'm missing?) Thanks in advance for helping me out, if anyone can spare a few minutes!
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Problem with Passing Checkbox Array Values to Email

Post by andyhoneycutt »

$_REQUEST['needs'] IS an array :)

You need to loop through it for actual values, or extract the values; regardless, it's not a string.

As an aside, if you serialize it you'll not need to unserialize it if you don't want to. It'll be readable, for the most part.

-Andy
jdb1981
Forum Newbie
Posts: 6
Joined: Mon Nov 17, 2008 1:55 pm

Re: Problem with Passing Checkbox Array Values to Email

Post by jdb1981 »

Um, ok, I'm not really sure how to do that... like I said, PHP is not really my thing, though I'd like to learn. Can you point me toward something that would teach me how to loop through or preferably extract the values and convert them to a usable string? There's got to be a function that does it, but I'm still lost with PHP...

Are the values being passed to the mailer script as it is, and needing to be extracted at that point? Or do I have to process them somehow in the first bit, beforehand?

Thanks, I was beginning to think no one was going to notice this question... I'll google on my own, but if you know of a good resource that could tell me what function and syntax and everything, please share...
jdb1981
Forum Newbie
Posts: 6
Joined: Mon Nov 17, 2008 1:55 pm

Re: Problem with Passing Checkbox Array Values to Email

Post by jdb1981 »

OK, well, I've gotten it to pass the variables into the email, but I can only get it to send the value of the last checked box to the actual email. I need it to send a comma separated or otherwise readable list of ALL checked values. Code follows:

Checkboxes: (10 like this, the values are passed to the mailer as "needs" array)

Code: Select all

Cake:<input type="checkbox" value="Cake" name="needs[]">
Rentals:<input type="checkbox" value="Rentals" name="needs[]">
And the bit of the mailer that processes them: (I've removed the other variables going into $embody, which makes up the email message, of course, for clarity's sake.)

Code: Select all

$needs = $_REQUEST['needs'];
 
for ($i=0;$i < count($needs); $i++)
$embody = "
        Needs:      $needs[$i]; \n";
I've googled around, and I actually ended up finding this post first in my results, which never happened to me before, but it seems like that should end the universe or something, if you google a subject and find your own question as the best response...

So, I know I'm on the right track, but there's something wrong with my syntax or something...

Any help? Thanks in advance!
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Problem with Passing Checkbox Array Values to Email

Post by andyhoneycutt »

A very simple way to turn an array in to a comma-separated string of values: implode function

Code: Select all

$needs_string = implode( ',' , $_REQUEST['needs'] );
-Andy
jdb1981
Forum Newbie
Posts: 6
Joined: Mon Nov 17, 2008 1:55 pm

Re: Problem with Passing Checkbox Array Values to Email

Post by jdb1981 »

Success! THANK YOU SO MUCH! I saw a lot of references to the implode and explode functions, but the examples were always passing the array contents to an echo or to an SQL database or something, not to another variable...

Now I've got to get the onsubmit sorted out, it's not keeping it from working properly, but it contains references to variables that don't exist anymore. (Left over from the template that was the basis of this thing.) I imagine this is not exactly best practice, and though this form is not exactly high-security, it might cause a problem somehow, I suppose. I'll presumably be back for help, if need be, and I'll post a full copy of the script here once it's done just in case anyone searching for a similar problem with the checkboxes needs it.

Thanks again!
jdb1981
Forum Newbie
Posts: 6
Joined: Mon Nov 17, 2008 1:55 pm

Re: Problem with Passing Checkbox Array Values to Email

Post by jdb1981 »

OK, one last thing: If none of the checkboxes in my form is checked, I'm getting an error message:

Warning: implode() [function.implode]: Invalid arguments passed... in /my/sites/location/mailer.php on line 13 (the line of the mailer script which contains the implode)

It still sends, but I know (think?) I need to use the following somewhere in that line:

Code: Select all

if(!empty($needs)
Then, I assume the result of the if statement would be to set the output of $needs_string to a string (like None or something) But I don't know where or how to accomplish it, syntax/punctuation-wise... I'm going to just try stuff, see if I come up with anything...

Thoughts? Thanks!
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Problem with Passing Checkbox Array Values to Email

Post by andyhoneycutt »

You're absolutely correct in your thinking. This should help:

Code: Select all

 
// default the needs string to "none".
$needs_string = "None";
if( !empty($_REQUEST['needs']) )
{
  $needs_string = implode( ',' , $_REQUEST['needs'] );
}
-Andy
jdb1981
Forum Newbie
Posts: 6
Joined: Mon Nov 17, 2008 1:55 pm

Re: Problem with Passing Checkbox Array Values to Email

Post by jdb1981 »

That works perfectly, thanks again. After I get the headers sorted out, I'll post the whole thing as a template, in case someone searching happens across it.

:mrgreen: You rule, and this forum is great, it makes me want to learn more PHP, unlike some forums that seem to discourage novice questions. I'll try to stick around and help someone else eventually!
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Problem with Passing Checkbox Array Values to Email

Post by andyhoneycutt »

Awesome. Glad I could help. And please do post your final solution, if someone else comes here, like you say, they'll have a full solution :)

-Andy
Post Reply