gdinari
gdinari

Reputation: 637

Submit mutiple checkbox selections in contact form

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!

Upvotes: 1

Views: 1153

Answers (1)

Cfreak
Cfreak

Reputation: 19319

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!

Upvotes: 2

Related Questions