I have been working on creating a PHP email form recently and cannot manage to send the checkbox values correctly. The form is one I found elsewhere and have been modifying. The HTML is fairly straightforward.
<label for="needs">I am looking for...</label>
<input name="needs[]" id="needs" type="checkbox" value="product list">A Product List<br>
<input name="needs[]" id="needs" type="checkbox" value="educational support">Educational Support<br>
<input name="needs[]" id="needs" type="checkbox" value="catering options">Catering Options<br>
<input name="needs[]" id="needs" type="checkbox" value="party ideas">Party Ideas<br>
<input name="needs[]" id="needs" type="checkbox" value="healthy alternatives">Healthy Alternatives<br>
<input name="needs[]" id="needs" type="checkbox" value="other">Other (please specify)<br>
As of right now, when I send it through the PHP it simply returns either a single selected value or if multiple are selected is says “array”. What I would like is for the message of the mail being sent to me to list the selected values. I have searched elsewhere, but have not found a situation the same as mine yet. Here is the PHP:
<?php
$msg=
'Name: '.$_POST['name'].'<br />
Category: '.$_POST['category'].'<br />
Email: '.$_POST['email'].'<br />
Phone: '.$_POST['telephone'].'<br />
Looking for: '.$_POST['needs'].'<br />
IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br />
Message:<br /><br />
'.nl2br($_POST['message']).'
';
?>
The rest of the code is setup and sending of the message. I realize that there are other ways of doing what I want, but would like to continue using the $msg that is included here.
Any help would be greatly appreciated, thanks.
Modified code:
$msg=
'Name: '.$_POST['name'].'<br />
Category: '.$_POST['category'].'<br />
Email: '.$_POST['email'].'<br />
Phone: '.$_POST['telephone'].'<br />
Looking for: '.(is_array($_POST['needs'])?implode("\n", $_POST['needs']):$_POST['needs']).'<br />
IP: '.$_SERVER['REMOTE_ADDR'].'<br /><br />
Message:<br /><br />
'.nl2br($_POST['message']).'
';
you should work with $_POST[‘needs’] as with array since it’s really an array. if you want just to get the list of checked checkboxes you can join the array to the string:
you can check whether certain option is checked or not:
and so on…
in your case i would do the following:
‘;
?>