Skip to content
Advertisement

Form Multiselect option is only returning 1 option in message

this is my first time making a form, and I have multi-select option but I think I’m missing out something because the form is working but when selecting multiple options the email I receive is only showing 1 option.

<form action="send.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Services</p>
<select data-placeholder="Responsible For...." multiple class="chosen-select full_width"  name="service" tabindex="-1">
<option value="Medical">Medical</option>
<option value="Finance">Finance</option>
<option value="Construction">Construction</option>
<option value="IT Services">IT Services</option>
</select>

<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>

send.php

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$service = $_POST['service'];
$message = $_POST['message'];
$formcontent=" From: $name n Type: $service n Message: $message";
$recipient = "itssonu4564@gmail.com";
$subject = "Message";
$mailheader = "From: $email rn";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

Advertisement

Answer

You need to do a small change in your HTML. Add a bracket pair to the name attribute, to make PHP it recognize it as an array.

<select multiple name="service[]">

Afterwards in PHP you can use

$service = implode(', ', $_POST['service']);

To get all services as a comma separated list.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement