如何通过php发送带有多个select的联系人表单
我正在尝试使用Ajax和PHP发送一个PHP联系人表单。在表单中,有一个select,它接受多个选项。我从联系人表单中得到的结果在收到的电子邮件中只打印了1个值 这就是我的代码的样子 HTML如何通过php发送带有多个select的联系人表单,php,jquery,html,Php,Jquery,Html,我正在尝试使用Ajax和PHP发送一个PHP联系人表单。在表单中,有一个select,它接受多个选项。我从联系人表单中得到的结果在收到的电子邮件中只打印了1个值 这就是我的代码的样子 HTML 首选卧室类型 一居室 两居室 三居室 四居室 我还通过ajax.serialize()发送了这个 PHP 通过以下代码更新您的foreach foreach ($_POST as $key => $value) { if (isset($fields[$key])) {
首选卧室类型
一居室
两居室
三居室
四居室
我还通过ajax.serialize()发送了这个
PHP
通过以下代码更新您的foreach
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";
}
}
}
$\u POST['room']
的值是一个数组。如果$value
是使用的数组,则可以使用获取所有值
非常感谢大家!非常感谢你的帮助!
<?php
/*
* CONFIGURE EVERYTHING HERE
*/
$name = $_POST['name'];
$email = $_POST['email'];
// configure
$from = 'Contact Form <abc@gmail.com>';
$reply = "$name<$email>";
$sendTo = 'Contact Form <abc@gmail.com>';
$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'];
}
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";
}
}
}
if (isset($fields[$key])) {
if (is_array($value)) {
$emailText .= "$fields[$key]: ".implode(', ',$value)."\n";
}
else {
$emailText .= "$fields[$key]: $value\n";
}
}