drdl18946 2018-04-25 11:19
浏览 40
已采纳

如何通过多个选择在php上发送联系表单

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 <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
=============================
";

    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
";
        }
    }

    // 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("
", $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!

  • 写回答

2条回答 默认 最新

  • douci1851 2018-04-25 11:25
    关注

    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)."
    ";
        }
        else {
            $emailText .= "$fields[$key]: $value
    ";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)