dqoqnmb163241 2017-09-25 16:16
浏览 32

如何通过ajax和php在电子邮件中发送数组

am a learner and I need your assistance. I have an online form for job application where you can list past jobs and enter referees. The problem is I want to send the details, which are in arrays but can't seem to catch a break. This is the html form

<fieldset>
    <h2>7: Referees</h2>
<div class="row clone3">    


 <div class="form-group">
    <label for="address">Name</label>
    <input type="text" class="form-control" id="name" name="name[]" placeholder="Name">
  </div>

    <div class="form-group">
    <label for="address">Contact (Email)</label>
    <input type="text" class="form-control" id="phone" name="phone[]" placeholder="Referee contact">
  </div>

     <div class="form-group">
    <label for="address">Contact (Mobile)</label>
    <input type="text" class="form-control" id="email" name="email[]" placeholder="Referee contact">
  </div>


    </div>  
             <div class="row"><div class="col-xs-12" align="center"><a href="#" class="add" rel=".clone3">Add Another Referee</a></div><br/><br/></div>


    <input type="button" name="previous" class="previous btn btn-default" value="Previous" />
    <input type="submit" name="submit" class="submit btn btn-success" value="Submit Application" id="form-submit" />
      <div id="msgSubmit" class="h3 text-center hidden"></div>
</fieldset>

This is the script to process the form

$("#contactForm").validator().on("submit", function (event) {
    if (event.isDefaultPrevented()) {
        // handle the invalid form...
        formError();
        submitMSG(false, "Did you fill in the form properly?");
    } else {
        // everything looks good!
        event.preventDefault();
        submitForm();
    }
});


function submitForm(){
    // Initiate Variables With Form Content
    var name = $("#name").val();
    var email = $("#email").val();
    var phone = $("#phone").val();


    $.ajax({
        type: "POST",
        url: "php/form-process.php",
        data: "name=" + name + "&email=" + email + "&phone=" + phone,
        success : function(text){
            if (text == "success"){
                formSuccess();
            } else {
                formError();
                submitMSG(false,text);
            }
        }
    });
}

function formSuccess(){
    $("#contactForm")[0].reset();
    submitMSG(true, "Message Sent! We'll get back shortly. Thank you! ")
}

function formError(){
    $("#contactForm").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
        $(this).removeClass();
    });
}

function submitMSG(valid, msg){
    if(valid){
        var msgClasses = "h3 text-center tada animated text-success";
    } else {
        var msgClasses = "h3 text-center text-danger";
    }
    $("#msgSubmit").removeClass().addClass(msgClasses).text(msg);
}

Below is the php code to send the form via email.

<?php

$errorMSG = "";

// NAME
if (empty($_POST["name"])) {
    $errorMSG = "Name is required ";
} else {
    $name = $_POST["name"];
}

// EMAIL
if (empty($_POST["email"])) {
    $errorMSG .= "Email is required ";
} else {
    $email = $_POST["email"];
}

// PHONE
if (empty($_POST["phone"])) {
    $errorMSG .= "Phone number is required ";
} else {
    $phone = $_POST["phone"];
}


$EmailTo = "xyz@gmail.com";
$Subject = "New Application From the Website";

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "
";
$Body .= "Email: ";
$Body .= $email;
$Body .= "
";
$Body .= "Phone: ";
$Body .= $phone;
$Body .= "
";


// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);

// redirect to success page
if ($success && $errorMSG == ""){
   echo "success";
}else{
    if($errorMSG == ""){
        echo "Something went wrong :(";
    } else {
        echo $errorMSG;
    }
}

?>

Now, the problem is the form only sends the email as one referee, even if I enter several referees.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画