开发者

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开发者_如何学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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜