以价值多个复选框ajax php的电子邮件

时间:2018-11-15 09:55:58

标签: php jquery ajax

你好,我有一个联系表单,用户填写了所有详细信息,他有多个复选框,提交后将发送电子邮件,其中将包含来自字段和复选框已被选中的值的所有值,我对php ajax进行了somethig公式化,我的代码:

$(document).ready(function (e){
    $("#frmContact").on('submit',(function(e){
        e.preventDefault();
         var myCheckboxes = new Array();
            $("input:checked").each(function() {
               data['myCheckboxes[]'].push($(this).val());
            });

        $("#mail-status").hide();
        $('#send-message').hide();
        $('#loader-icon').show();
        $.ajax({
            url: "oferta.php",
            type: "POST",
            dataType:'json',
            data: {
            "den_org":$('input[name="den_org"]').val(),
            myCheckboxes:myCheckboxes,
            "pers_cont":$('input[name="pers_cont"]').val(),
            "localitate":$('input[name="localitate"]').val(),
            "email2":$('input[name="email2"]').val(),
            "tlf":$('input[name="tlf"]').val(),
            "nr_local":$('input[name="nr_local"]').val(),
            "nr_ang":$('input[name="nr_ang"]').val(),
            "dom_activ":$('input[name="dom_activ"]').val(),
            "alte":$('input[name="alte"]').val(),
            "g-recaptcha-response":$('textarea[id="g-recaptcha-response"]').val()},             
            success: function(response){
            $("#mail-status").show();
            $('#loader-icon').hide();
            if(response.type == "error") {
                $('#send-message').show();
                $("#mail-status").attr("class","error");                
            } else if(response.type == "message"){

                $('#send-message').hide();
                $("#mail-status").attr("class","success");                          
            }
            $("#mail-status").html(response.text);  
            },
            error: function(){} 
        });
    }));
});

即使在这里我也在google上搜索,仍然无法将复选框的值传递到文件中,该文件是我使用php发送电子邮件的。 这是html的代码:

<form id="frmContact" action="" method="POST" novalidate="novalidate">
<div class="col-sm-6">
    <div class="form-group">
        <label >Certificari *</label>
        <ul class="c_list">
        <li><input type="checkbox" class="i11" name="myCheckboxes[]" value="ISO_9001"> ISO 9001</li>
        <li><input type="checkbox" class="i11" name="myCheckboxes[]" value="ISO_140011"> ISO 14001</li>
        <li><input type="checkbox" class="i11" name="myCheckboxes[]" value="ISO_45001"> ISO 45001</li>
        <li><input type="checkbox" class="i11" name="myCheckboxes[]" value="Other"> Other</li>
        </ul>
</div>
 </div>

对于php,我有类似的东西:

<?php
  if($_POST)
  {
   include('config.php');

$den_org      = filter_var($_POST["den_org"], FILTER_SANITIZE_STRING);
$pers_cont      = filter_var($_POST["pers_cont"], FILTER_SANITIZE_STRING);
$localitate     = filter_var($_POST["localitate"], FILTER_SANITIZE_STRING);
$email2     = filter_var($_POST["email2"], FILTER_SANITIZE_STRING);
$nr_ang     = filter_var($_POST["nr_ang"], FILTER_SANITIZE_STRING);
$tlf     = filter_var($_POST["tlf"], FILTER_SANITIZE_STRING);
$dom_activ     = filter_var($_POST["dom_activ"], FILTER_SANITIZE_STRING);
$alte     = filter_var($_POST["alte"], FILTER_SANITIZE_STRING);
$nr_local     = filter_var($_POST["nr_local"], FILTER_SANITIZE_STRING);
$iso     = filter_var($_POST["myCheckboxes"], FILTER_SANITIZE_STRING);

if(empty($den_org)) {
    $empty[] = "<b>denumirea organizatiei</b>";     
}
if(empty($pers_cont)) {
    $empty[] = "<b>persoana de contact</b>";
}
if(empty($localitate)) {
    $empty[] = "<b>localitate</b>";
}   
// if(empty($email2)) {
//  $empty[] = "<b>email</b>";
// }
if(empty($nr_ang)) {
    $empty[] = "<b>numar angajati</b>";
}

if(!empty($empty)) {
    $output = json_encode(array('type'=>'error', 'text' => implode(", ",$empty) . ' Required!'));
    die($output);
}

// if(!filter_var($email2, FILTER_VALIDATE_EMAIL)){ //email validation
//     $output = json_encode(array('type'=>'error', 'text' => '<b>'.$email2.'</b> is an invalid Email, please correct it.'));
//  die($output);
// }

//reCAPTCHA validation
if (isset($_POST['g-recaptcha-response'])) {

    require('component/recaptcha/src/autoload.php');        

    $recaptcha = new \ReCaptcha\ReCaptcha(SECRET_KEY, new \ReCaptcha\RequestMethod\SocketPost());

    $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);

      if (!$resp->isSuccess()) {
            $output = json_encode(array('type'=>'error', 'text' => '<b>Captcha</b> Validation Required!'));
            die($output);               
      } 
}

$toEmail = "myemail@gmail.com";
$mailHeaders = "From: " . $pers_cont. "<" . $email2 . ">\r\n";
$mailContent = "Persoana de contact:". $pers_cont . ">\r\n"."Denumire organizatie: ". $den_org .">\r\n"."Localitate: ". $localitate .">\r\n"."Email: ". $email2 .">\r\n"."Numar Angajati: ". $nr_ang .">\r\n"."Telefon: ".$tlf.">\r\n"."Domeniul de activitate: ". $dom_activ .">\r\n"."Alte informatii pe care le considerati importante: ". $alte .">\r\n"."Numar locatii: ".$iso.">\r\n";
if (mail($toEmail, "O noua oferta completata", $mailContent, $mailHeaders)) {
    $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$pers_cont .', thank you. We will get back to you shortly.'));
    die($output);
} else {
    $output = json_encode(array('type'=>'error', 'text' => 'Unable to send email, please contact administrator'));
    die($output);
     }
    }
   ?>

2 个答案:

答案 0 :(得分:0)

您正在使用filter_var, 但是

$ _ POST [“ myCheckboxes”]是一个数组(这就是php用[]处理帖子名称的方式)

看看:

http://php.net/manual/en/function.filter-var-array.php

答案 1 :(得分:0)

尝试

           var myCheckboxes = [];
            $("input[name='myCheckboxes[]']:checked").each(function () {
                row = $(this).closest("li");
                myCheckboxes.push($(this).val());
            });

在这里您可以获得myCheckboxes = [1,2,3]之类的值。使用以下命令将myCheckboxes传递给php文件:  ajax,然后在您的php文件中将其爆炸     $ a = explode(',',myCheckboxes)  然后使用for循环

for($i=0;i<$a.length;$i++){
your code here with checkbox value as $a[$i]
}