开发者

Submit mutiple checkbox selections in contact form

开发者 https://www.devze.com 2023-01-08 17:15 出处:网络
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:

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开发者_如何学JAVA']);
  }

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!


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消