多个复选框值未发送到PHP中的电子邮件

时间:2019-03-30 06:54:44

标签: php email bootstrap-4

我刚开始使用PHP编码。目前,当我提交表单的每个字段时,我的复选框有问题,但我没有收到多个复选框的值。刚收到电子邮件中的“ ARRAY”文本。请在下面检查我的代码....................................... ..................

<div class="col-sm-12">
    <div class="form-group text-center pl-3">
        <div class="custom-control custom-checkbox custom-control-inline">
            <input type="checkbox" class="custom-control-input" name="healthcover[]" value="Life Insurance" id="LifeInsurance">
            <label class="custom-control-label mr-3" for="LifeInsurance" data-toggle="tooltip" data-placement="top" data-original-title="Life insurance is a type of insurance contract which pays out a lump sum to your dependants should you pass away during the term of the contract. The cost of a policy is determined by a number of factors including your age, health and lifestyle.">Life Insurance</label>
        </div>
        <div class="custom-control custom-checkbox custom-control-inline">
            <input type="checkbox" class="custom-control-input" name="healthcover[]" value="Critical Illness Cover" id="CriticalIllnessCover">
            <label class="custom-control-label mr-3" for="CriticalIllnessCover" data-toggle="tooltip" data-placement="top" data-original-title="Critical illness cover is a type of life insurance policy that offers protection in the event of a serious illness or injury. If a policyholder suffers from a specific illness or injury, a payout is made by an insurance provider in the form of a lump-sum, which can be tax-free">Critical Illness Cover</label>
        </div>
        <div class="custom-control custom-checkbox custom-control-inline">
            <input type="checkbox" class="custom-control-input" name="healthcover[]" value="Income Protection" id="IncomeProtection">
            <label class="custom-control-label mr-3" for="IncomeProtection" data-toggle="tooltip" data-placement="top" data-original-title="Income protection insurance (sometimes known as permanent health insurance) is a long-term insurance policy designed to help you if you can’t work because you’re ill or injured. It ensures you continue to receive a regular income until you retire or are able to return to work.">Income Protection</label>
        </div>
        <div class="custom-control custom-checkbox custom-control-inline">
            <input type="checkbox" class="custom-control-input" name="healthcover[]" value="Private Medical" id="PrivateMedical">
            <label class="custom-control-label mr-3" for="PrivateMedical" data-toggle="tooltip" data-placement="top" data-original-title="Health insurance is an insurance policy designed to cover the cost of private medical treatment. ... You can buy different types of policies that offer various levels of cover, at varying costs. This could include fast-track diagnostics for cancer or access to different cancer treatments not currently available on the NHS.">Private Medical</label>
        </div>
    </div>
</div>





<?php

// configure
$from = 'Contact Form <email@gmail.com>';
$sendTo = 'Contact Form <email@gmail.com>';
$subject = 'New message from website.com';
$fields = array('image_radio' => 'Free Gift Name', 'member' => 'Who’s the cover for','healthcover' => 'What type of cover do you need', 'smoke' => 'Have you smoked tobacco in the last 12 months' , 'illness' => 'Have you had any life threatening illnesses in the last 10 years','Message' => 'Message','name' => 'Full Name','email' => 'Email','contactNo' => 'Contact Number','occupation' => 'Occupation'); // array variable name => Text to appear in the email
$okMessage = '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';

// let's do the sending

try
{
    $emailText = "You have new message from contact form\n=============================\n";

    foreach ($_POST as $key => $value) {

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

    $headers = array('Content-Type: text/plain; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
    );

    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

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 {
    echo $responseArray['message'];
}

这是通过电子邮件输出的

You have new message from contact form
=============================
Free Gift Name: Apple Watch
Who’s the cover for: Joint
What type of cover do you need: Array
Have you smoked tobacco in the last 12 months: Yes
Have you had any life threatening illnesses in the last 10 years: Yes
Message:
Full Name: gg
Email: gg@g.com
Contact Number: 1232
Occupation: blahh

2 个答案:

答案 0 :(得分:0)

您可以使用 implode 函数将复选框多选为字符串。

var nodes = new vis.DataSet([
    {label: "Pop"},
    {label: "Alternative"},
    {label: "Rock"},
    {label: "Jazz"},
    {label: "Hits"},
    {label: "Dance"},
    {label: "Metal"},
    {label: "Experimental"},
    {label: "Rap"},
    {label: "Electronic"},
]);
var edges = new vis.DataSet();

var container = document.getElementById('bubbles');
var data = {
    nodes: nodes,
    edges: edges
};

var options = {
    nodes: {borderWidth:0,shape:"circle",color:{background:'#F92C55', highlight:{background:'#F92C55', border: '#F92C55'}},font:{color:'#fff'}},
    physics: {
        stabilization: false,
        minVelocity:  0.01,
        solver: "repulsion",
        repulsion: {
            nodeDistance: 40
        }
    }
};
var network = new vis.Network(container, data, options);


// Events
network.on("click", function(e) {
    if (e.nodes.length) {
        var node = nodes.get(e.nodes[0]);
        // Do something
        nodes.update(node);
    }
});
export { nodes, edges, container, data, options, network };

尝试

foreach ($_POST as $key => $value) {

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

您可以使用br或其他任何标记代替逗号

foreach ($_POST as $key => $value) {

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

答案 1 :(得分:0)

html

<div class="col-sm-12">
    <div class="form-group text-center pl-3">
        <div class="custom-control custom-checkbox custom-control-inline">
            <input type="checkbox" class="custom-control-input" name="healthcover[]" value="Life Insurance" id="LifeInsurance">
            <label class="custom-control-label mr-3" for="LifeInsurance" data-toggle="tooltip" data-placement="top" data-original-title="Life insurance is a type of insurance contract which pays out a lump sum to your dependants should you pass away during the term of the contract. The cost of a policy is determined by a number of factors including your age, health and lifestyle.">Life Insurance</label>
        </div>
        <div class="custom-control custom-checkbox custom-control-inline">
            <input type="checkbox" class="custom-control-input" name="healthcover[]" value="Critical Illness Cover" id="CriticalIllnessCover">
            <label class="custom-control-label mr-3" for="CriticalIllnessCover" data-toggle="tooltip" data-placement="top" data-original-title="Critical illness cover is a type of life insurance policy that offers protection in the event of a serious illness or injury. If a policyholder suffers from a specific illness or injury, a payout is made by an insurance provider in the form of a lump-sum, which can be tax-free">Critical Illness Cover</label>
        </div>
        <div class="custom-control custom-checkbox custom-control-inline">
            <input type="checkbox" class="custom-control-input" name="healthcover[]" value="Income Protection" id="IncomeProtection">
            <label class="custom-control-label mr-3" for="IncomeProtection" data-toggle="tooltip" data-placement="top" data-original-title="Income protection insurance (sometimes known as permanent health insurance) is a long-term insurance policy designed to help you if you can’t work because you’re ill or injured. It ensures you continue to receive a regular income until you retire or are able to return to work.">Income Protection</label>
        </div>
        <div class="custom-control custom-checkbox custom-control-inline">
            <input type="checkbox" class="custom-control-input" name="healthcover[]" value="Private Medical" id="PrivateMedical">
            <label class="custom-control-label mr-3" for="PrivateMedical" data-toggle="tooltip" data-placement="top" data-original-title="Health insurance is an insurance policy designed to cover the cost of private medical treatment. ... You can buy different types of policies that offer various levels of cover, at varying costs. This could include fast-track diagnostics for cancer or access to different cancer treatments not currently available on the NHS.">Private Medical</label>
        </div>
    </div>
</div>


<?php

// configure
$from = 'Contact Form <email@gmail.com>';
$sendTo = 'Contact Form <email@gmail.com>';
$subject = 'New message from website.com';
$fields = array('image_radio' => 'Free Gift Name', 'member' => 'Who’s the cover for','healthcover' => 'What type of cover do you need', 'smoke' => 'Have you smoked tobacco in the last 12 months' , 'illness' => 'Have you had any life threatening illnesses in the last 10 years','Message' => 'Message','name' => 'Full Name','email' => 'Email','contactNo' => 'Contact Number','occupation' => 'Occupation'); // array variable name => Text to appear in the email
$okMessage = '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';

// let's do the sending

try
{
    $emailText = "You have new message from contact form\n=============================\n";

    foreach ($_POST as $key => $value) {

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

    $headers = array('Content-Type: text/plain; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
    );

    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

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 {
    echo $responseArray['message'];
}

现在输出

You have new message from contact form
=============================
Free Gift Name: Apple Watch
Who’s the cover for: Individual
What type of cover do you need:Life Insurance,Critical Illness CoverHave you smoked tobacco in the last 12 months: No
Have you had any life threatening illnesses in the last 10 years: Yes
Message:
Full Name: rintu
Email: gg@g.com
Contact Number: 1232
Occupation: ghghh

期望输出

You have new message from contact form
=============================
Free Gift Name: Apple Watch
Who’s the cover for: Individual
What type of cover do you need:Life Insurance,Critical Illness Cover
Have you smoked tobacco in the last 12 months: No
Have you had any life threatening illnesses in the last 10 years: Yes
Message:
Full Name: rintu
Email: gg@g.com
Contact Number: 1232
Occupation: ghghh