我无法将多个复选框的值发送到php电子邮件脚本

时间:2017-11-24 19:21:18

标签: php ajax

我的表单包含多个复选框,我需要将其值发送到电子邮件,但我无法通过ajax发送到电子邮件脚本。

但是如果没有ajax php代码正在运行,请建议我遗漏的地方 以下是我的HTML代码

<form enctype="multipart/form-data">

 <div class="col-md-12 col-sm-12"><input type="text" id="name" name="name" placeholder="Name:*" class="form-control" required/></div>
 <div class="col-md-12 col-sm-12"><input type="email" id="email" name="email" placeholder="Email:*" class="form-control" required/></div>
 <div class="col-md-12 col-sm-12"><input type="text" id="phone" name="phone" placeholder="Phone:*" class="form-control" required/></div>
  <p>Vehicle Type You're Shipping?:*</p>

 <div class="input-holder field-checkbox">



                        <div class="field-element ">

                            <input type="checkbox" id="" name="checkbox[]" value="Car, Truck Or SUV" class="required ">
                            <label for="1dfb36b53196072fe797a082d9892947-3-0">Car, Truck Or SUV</label>
                        </div>

                        <div class="field-element ">

                            <input type="checkbox" id="" name="checkbox[]" value="Motorcycle Or ATV" class="required ">
                            <label for="1dfb36b53196072fe797a082d9892947-3-1">Motorcycle Or ATV</label>
                        </div>

                        <div class="field-element ">

                            <input type="checkbox" id="" name="checkbox[]" value="RV Or Motorhome" class="required ">
                            <label for="1dfb36b53196072fe797a082d9892947-3-2">RV Or Motorhome</label>
                        </div>

                        <div class="field-element ">

                            <input type="checkbox" id="" name="checkbox[]" value="Boat/Yacht, Jet Ski" class="required ">
                            <label for="1dfb36b53196072fe797a082d9892947-3-3">Boat/Yacht, Jet Ski</label>
                        </div>

                        <div class="field-element ">

                            <input type="checkbox" id="" name="checkbox[]" value="Trailer" class="required ">
                            <label for="1dfb36b53196072fe797a082d9892947-3-4">Trailer</label>
                        </div>

                        <div class="field-element ">

                            <input type="checkbox" id="" name="checkbox[]" value="Other" class="required ">
                            <label for="1dfb36b53196072fe797a082d9892947-3-5">Other</label>
                        </div>

                              发送

<center> <div class="result" id="result"style="color:red;font-weight: 900;background: #fff; "></div></center>


 </form>

我有这个代码用于ajax

<script>
function submit_user(){


    var name = document.getElementById('name').value;
    var email = document.getElementById('email').value;
    var phone = document.getElementById('phone').value;
    var country = document.getElementById('country').value;
    var current = document.getElementById('current').value;
    var model = document.getElementById('model').value;
    var enquiry = document.getElementById('enquiry').value;
    var myCheckboxes = new Array();
    $("input:checkebox").each(function() {
    data['myCheckboxes[]'].push($(this).val());
    });

    $.ajax({
      type: "POST",
      url: "script.php?name="+name+"&email="+email+"&phone="+phone+"&checkbox="+myCheckboxes+"&country="+country+"&current="+current+"&model="+model+"&enquiry="+enquiry,
      success:function(data) {
          $( ".result" ).html(data); 

      }
    });
 //  alert("2s");


}

我有这个PHP脚本

    <?php
if (!empty($_GET['name']) && !empty($_GET['email']) && !empty($_GET['phone'] ) && !empty($_GET['country']) && !empty($_GET['current']) && !empty($_GET['model'])) {
    $message       = "New Email from landing Page";
    $name          = $_GET['name']; 
    $email         = $_GET['email']; 
    $phone         = $_GET['phone'];
    $country       =$_GET['country'];
    $current       =$_GET['current'];
    $model         =$_GET['model'];
    $enquiry       =$_GET['enquiry'];
    $from          = "From: Booking from Landing Page";
    $to            = "email@gmail.com";
    $subject       = " You Got a Booking From Landing Page!";
    if (isset($_GET['myCheckboxes']) && !empty($_GET['myCheckboxes']))        {
        foreach($_GET['myCheckboxes'] as $selected ){
        $MESSAGE_BODY .= $selected."\r\n";
    }
    }
    $mail              = "
    Info:               ".$message."
    Name:               ".$name."
    email:              ".$email."
    Phone:              ".$phone."
    checkbox            ".$MESSAGE_BODY."
    Country:            ".$country."
    Currrent:           ".$current."
    Model:              ".$model."
    Message:            ".$enquiry."";
    mail($to, $subject, $mail, $from);
    //readfile("thank-you.html");    
    //exit;
echo "Thank You, I will get back to you ASAP";
}else{
    //readfile("error.html");}
    echo "something is incorrect, please retry";

}
?>

1 个答案:

答案 0 :(得分:0)

尝试更改 -

var myCheckboxes = new Array();
$("input:checkebox").each(function() {
data['myCheckboxes[]'].push($(this).val());
});

var myCheckboxes = new Array();
$("input:checkbox:checked").each(function() {
myCheckboxes.push($(this).val());
});

(1)input:checkebox应为input:checkbox
(2)添加:checked,因为您只想发送选中的复选框,而不是每个复选框 (3)您想要.push()myCheckboxes,而不是data['myCheckboxes[]']