douhan9191 2016-08-23 10:40
浏览 48
已采纳

如何将带有ajax的表单复选框中的值传递给邮件

I need help with value sending to mail from form on html page, there is 3 file: test.html with form group checkbox, contact.php and contact.js

This is the contact.js:

$(function () {

$('#contact-form').validator();

$('#contact-form').on('submit', function (e) {
    if (!e.isDefaultPrevented()) {
        var url = "contact.php";

        $.ajax({
            type: "POST",
            url: url,
            data: $(this).serialize(),
            success: function (data)
            {
                var messageAlert = 'alert-' + data.type;
                var messageText = data.message;

                var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';
                if (messageAlert && messageText) {
                    $('#contact-form').find('.messages').html(alertBox);
                    $('#contact-form')[0].reset();
                }
            }
        });
        return false;
    }
})
});

and this is contact.php for processing form to mail

$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message' 'checkboxes' => 'Colors');

form group is:

<div class="form-group">
<div class="col-md-12">
    <label>Colors:</label>
</div>
<div class="col-md-12">
    <label class="checkbox-inline" for="form_checkboxes1">
        <input type="checkbox" name="checkboxes[]" id="form_checkboxes1" value="Blue"> + Blue
    </label>
    <label class="checkbox-inline" for="form_checkboxes2">
        <input type="checkbox" name="checkboxes[]" id="form_checkboxes2" value="Green"> + Green
    </label>
    <label class="checkbox-inline" for="form_checkboxes3">
        <input type="checkbox" name="checkboxes[]" id="form_checkboxes3" value="Orange"> + Orange
    </label>
</div>

the problem is when choose an option for Colors or choose every three colors after mail sent result is Colors: array

now, I trie to change line in contact.php adding

$checkboxes = implode(',',$_POST['checkboxes']);

but no succsess

rest of kod in contact.php is:

try{
    $emailText = "text";
    foreach ($_POST as $key => $value) {
        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value
";}
        }
        mail($sendTo, $subject, $emailText, "From: " . $from);
        $responseArray = array('type' => 'success', 'message' => $okMessage);
    }
  • 写回答

1条回答 默认 最新

  • douzao2590 2016-08-23 10:58
    关注

    Try this code it's works for you. your code not working because of you have pass array in color so you need to implode your array and then paas to you mail. i have try this and it's works fine.

    try{
        $emailText = "text";
        foreach ($_POST as $key => $value) {
    
            if (is_array($value)) {
               $emailText .= "$fields[$key]: " . implode(',', $value) . "
    ";
                continue;
            }
            if (isset($fields[$key])) {
                $emailText .= "$fields[$key]: $value
    ";}
            }
            mail($sendTo, $subject, $emailText, "From: " . $from);
            $responseArray = array('type' => 'success', 'message' => $okMessage);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?