views:

23

answers:

1

Hi, Im new to PHP and Im having trouble getting a contact from to submit multiple checkbox selections.

Here is the part of my php that handles the selections:

if(trim($_POST['interest']) == '') {

$hasError = true; } else { $interest = trim($_POST['interest']); }

And this is the part of my HTML:

<label for="interest"><strong>I'm interested in:</strong></label>
<input type="checkbox" size="" name="interest" value="Photography" /> Photography
<input style="margin-left: 20px;" type="checkbox" size="" name="interest" value="Editorial" /> Editorial
<input style="margin-left: 20px;" type="checkbox" size="" name="interest" value="Other" /> Other

I would appreciate any help on this issue. Thanks in advance!

A: 

You need to send it as an array.

In the HTML add [] to each name field like so:

<input type="checkbox" name="interest[]" value="...">

In the PHP:

if( isset( $_POST['interest'] ) && is_array($_POST['interest']) ) {
    foreach( $_POST['interest'] as $value ) {
        //each $value is a selected value from the form
    }
 }

Hope that helps!

Cfreak
Hey Cfreak, I tried the coding just as you have it to me, but when I received the message from the contact form, the interest field is empty.Also, I noticed that you have an extra "}" in the coding, which cause an error. I removed it, I got no error, but the multiple submission still didn't work. I got an empty "interest" field in email. Any other ideas?Thanks
gdinari
Oops. There should have been an opening { for the if statement.My code won't put into into the email for you, the code I showed just shows you how to capture it. How are you trying to put it in the email?
Cfreak
Sorry for the late reply. I just want to list the multiple checkbox selections like so: Interest: Photography, Editorial, Other. This is depending on what selection(s) a visitor chooses. I hope you can help. Let me know if you need any more of my codes. Thanks.
gdinari