0

我有一个 javascript 函数来操作一些表单数据并将 POST 构建到 php 页面。js 执行 xmlHTTP.open 并发送到构建的 url。

php 页面应该接受参数并发送电子邮件,但它似乎不接受参数。

我知道我在这里遗漏了一些愚蠢的东西,但是我还有其他类似的页面,但是参数更少并且它们工作正常。

<script>
function submitReg()
{
    var fn = document.getElementById("textfname").value;
    var ln = document.getElementById("textlname").value;
    var e = document.getElementById("textemail").value;
    var p = document.getElementById("textphone").value;
    var pr = document.getElementById("promocode").value;
    var c1 = document.getElementById("st-1").innerHTML + "_" 
    + document.getElementById("c-  1").innerHTML;
    if (document.getElementById("st-2").innerHTML != "")
    {
        var c2 = document.getElementById("st-2").innerHTML + "_" + 
    document.getElementById("c-2").innerHTML;
    }
    if (document.getElementById("st-3").innerHTML != "")
    {
        var c3 = document.getElementById("st-3").innerHTML + "_" + 
    document.getElementById("c-3").innerHTML;
    }
    var myURL = "form-to-email.php?" + "fn=" + fn + "&ln=" + ln + "&e=" + e + 
    "&p=" + p + "&pr=" + pr + "&c1=" + c1 + "&c2=" + c2 + "&c3=" + c3;


    xmlhttp.open("POST", (encodeURI(myURL)), true);
    xmlhttp.send();
}

</script>

和 PHP 代码:

<?php

$fname = $_POST["fn"];
$lname = $_POST["ln"];
$email = $_POST["e"];
$phone = $_POST["p"];
$promo = $_POST["pr"];
$county1 = $_POST["c1"];
$county2 = $_POST["c2"];
$county3 = $_POST["c3"];



$email_from = 'support@somesite.com';

$email_subject = "Someone filled out the Splash page";

$email_body = "You have received a new form submission from the user $visitor_email.\n".
                        "Here it is:\n ".
            "First Name: $fname \n".
"Last Name: $lname \n".
"Email: $email \n".
"Phone: $phone \n".
"Promo Code: $promo \n".
"First County: $county1 \n".
"Second County(if Any): $county2 \n".
"Third County(if Any): $county3 \n";

$to = "user@gmail.com";

$headers = "From: $email_from \r\n";

mail($to,$email_subject,$email_body,$headers);
Header("Location: thankyou.html");

?>
4

1 回答 1

0

问题是您正在发送一个设置了很多 GET 参数的空 POST,而您的 php 代码需要 POST 参数。

正确使用帖子:

var myURL = "form-to-email.php";
var params = "fn=" + encodeURIComponent(fn) + "&ln=" + encodeURIComponent(ln) + "&e=" + encodeURIComponent(e) + "&p=" + encodeURIComponent(p) + "&pr=" + encodeURIComponent(pr) + "&c1=" + encodeURIComponent(c1) + "&c2=" + encodeURIComponent(c2) + "&c3=" + encodeURIComponent(c3);
xmlhttp.open("POST", myURL, true);
xmlhttp.send(params);   

或者准备 PHP 接收“GET”参数:

$fname = $_GET["fn"];

或者如果它们是 GET 或 POST 参数,请不要从 php 方面担心:

$fname = $_REQUEST["fn"];
于 2013-02-18T23:00:09.150 回答