Reputation: 59
I'm trying to send a PHP contact form using Ajax and PHP. In the form, there's a select which take in multiple options. The result I get from the contact form only has 1 value printed in the received email.
This is how my code looks like.
HTML
<select multiple="multiple" name="room[]" id="room" required="required" data-error="Please select your preferred bedroom type." size="5">
<option value="" disabled>PREFERRED BEDROOM TYPE</option>
<option value="1 Bedroom">1 Bedroom</option>
<option value="2 Bedroom">2 Bedroom</option>
<option value="3 Bedroom">3 Bedroom</option>
<option value="4 Bedroom">4 Bedroom</option>
</select>
I have also sent this via ajax .serialize()
PHP
<?php
/*
* CONFIGURE EVERYTHING HERE
*/
$name = $_POST['name'];
$email = $_POST['email'];
// configure
$from = 'Contact Form <[email protected]>';
$reply = "$name<$email>";
$sendTo = 'Contact Form <[email protected]>';
$subject = 'New message from Stirling Residences Contact Form';
$fields = array('name' => 'Name', 'mobile' => 'Mobile', 'email' => 'Email', 'room' => 'Bedroom Type', 'message' => 'Message');
$okMessage = 'Contact form successfully submitted. Thank you, we will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
try
{
if(count($_POST) == 0) throw new \Exception('Form is empty');
$emailText = "You have a new message from your contact form\n=============================\n";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
// All the neccessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $reply,
'Return-Path: ' . $reply,
);
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];
}
Any help will be deeply appreciated!
Upvotes: 1
Views: 78
Reputation: 19780
The value of $_POST['room']
is an array. You could use implode()
to get all values, if the $value
is an array using is_array()
.
if (isset($fields[$key])) {
if (is_array($value)) {
$emailText .= "$fields[$key]: ".implode(', ',$value)."\n";
}
else {
$emailText .= "$fields[$key]: $value\n";
}
}
Upvotes: 2
Reputation: 1598
update your foreach
by following code
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
switch($key){
case 'room' :
$emailText .= "$fields[$key]: ". @implode(', ', $value)."\n";
break;
default :
$emailText .= "$fields[$key]: $value\n";
}
}
}
Upvotes: 1