如何在JS中调用PHP mail()函数?

时间:2014-03-12 00:49:00

标签: javascript php jquery html ajax

我有一个困境。我为我正在建立的网站创建了这个订单请求页面,以下是它的工作原理:

  1. 您检查/取消选中核心项目,并根据其状态(活动或非活动),它会将true / false值应用于相应order.js中的布尔值。同样,它会为其他项目和您的信息获取所需的单位数,并将其应用于变量。
  2. 它将这些变量分组到数组中,现在,它们console.log()
  3. 这就是麻烦来的地方......这是order.js档案的一个片段。

    function compileInfo() {
    
        console.log("compileInfo active");
    
        var name = "Name: " + $("#name").val() + "\n";
        var email = "Email: " + $("#email").val() + "\n";
        var phone = "Phone: " + $("#phone").val() + "\n";
        var weddingDate = "Wedding Date: " + $("#date").val() + "\n";
        var comments = "Comments: " + $("#comments").val() + "\n";
    
        var base = "Base Experience: " + $("#base").hasClass("active") + "\n";
        var special = "Special Edition: " + $("#special").hasClass("active") + "\n";
        var teaser = "Teaser Trailer: " + $("#teaser").hasClass("active") + "\n";
        var raw = "Raw Footage: " + $("#raw").hasClass("active") + "\n";
    
        var standard = "Standard Shipping: " + $("#standard").hasClass("active") + "\n";
        var expedited = "Expedited Shipping: " + $("#expedited").hasClass("active") + "\n";
    
        var dvd = "Standard DVD: " + a + "\n";
        var br = "Standard Blu-Ray: " + b + "\n";
        var dvdSe = "Special DVD: " + x + "\n";
        var brSe = "Special Blu-Ray: " + y + "\n";
    
        var info = new Array();
            info[0] = name;
            info[1] = email;
            info[2] = phone;
            info[3] = weddingDate;
            info[4] = comments;
    
        var services = new Array();
            services[0] = base;
            services[1] = special;
            services[2] = teaser;
            services[3] = raw;
    
        var delivery = new Array();
            delivery[0] = standard;
            delivery[1] = expedited;
    
        var extras = new Array();
            extras[0] = dvd;
            extras[1] = br;
            extras[2] = dvdSe;
            extras[3] = brSe;
    
        var dataVar = info + "\n" + services + "\n" + delivery + "\n" + extras;
        var dataVarJSON = JSON.stringify(dataVar);
    
        console.log(dataVar);
    
        $.ajax({
            type: "POST",
            url: "order.php",
            data: {data : dataVarJSON},
    
            success: function() {
                console.log("SUCCESS");
            }
        });
    }
    
    function validate() {
        var name = $("#name").val();
        var email = $("#email").val();
        var weddingDate = $("#date").val();
    
        if (name === "" || email == "" || weddingDate == "") {
            alert("You must complete all required fields to send this request.");
        } else {
            console.log("working");
            compileInfo();
            return true;
        }
    
    }
    

    这是我收到的PHP:

    <?php
        header('content-type: application/json; charset=utf-8');
        header("access-control-allow-origin: *");
    
        $body = json_decode(stripslashes($_POST['data']));
    
        $to = "thekevinhaube@gmail.com";
        $subject = "Order Request";
    
        function sendInfo() {
            mail($to, $subject, $body);
        }
     ?>
    

    现在,我远非PHP专家。事实上,这是我第一次遇到它。我怎样才能发送它。似乎POST似乎没问题,但是没有发送到列出的电子邮件地址。任何和所有的帮助表示赞赏!再次,这是我第一次使用PHP,所以......

1 个答案:

答案 0 :(得分:0)

您需要为sendInfo提供一些参数:

function sendInfo($to, $subject, $body) {
  ...
}

并且不要在您的localhost上尝试mail(),如果可能的话,尝试在线。

相关问题