簡體   English   中英

如何通過多個選擇通過PHP發送聯系表單

[英]How do I send a contact form over php with multiple select

我正在嘗試使用Ajax和PHP發送一個PHP聯系人表格。 在表單中,有一個帶有多個選項的選擇。 我從聯系表中獲得的結果在收到的電子郵件中僅顯示1個值。

這就是我的代碼的樣子。

的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>

我也通過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\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'];
}

任何幫助將不勝感激!

$_POST['room']是一個數組。 如果$value是使用is_array()的數組,則可以使用implode()獲取所有值。

if (isset($fields[$key])) {
    if (is_array($value))  {
        $emailText .= "$fields[$key]: ".implode(', ',$value)."\n";
    }
    else {
        $emailText .= "$fields[$key]: $value\n";
    }
}

通過以下代碼更新您的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";
        }        
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM