我的AJAX表单没有发送到我的PHP

时间:2016-03-25 09:35:22

标签: javascript php jquery ajax

function sendForm(e) {
    e.preventDefault();
    var formData = new FormData();

    formData.append('percentageOfMessages', $('#percentageOfMessages').val());

    if ($('RemoveDeletedAccounts').prop('checked')) {
        formData.append('RemoveDeletedAccounts', "1");
    } else {
        formData.append('RemoveDeletedAccounts', "0");
    }

    if ($('RemoveNoReply').prop('checked')) {
        formData.append('RemoveNoReply', "1");
    } else {
        formData.append('RemoveNoReply', "0");
    }

    if ($('RemoveNoResponse').prop('checked')) {
        formData.append('RemoveNoResponse', "1");
    } else {
        formData.append('RemoveNoReply', "0");
    }

    formData.append('minMatchPercent', $('#minMatchPercent').val());
    formData.append('minDistance', $('#minDistance').val());
    formData.append('maxDistance', $('#maxDistance').val());

    formData.append('blacklistUsernames', $('#blacklistUsernames').val());

    formData.append('pickuplineText', $('#pickuplineText').val());

    formData.append('userEmail', $('#userEmail').val());

    formData.append('Username', $('#Username').val());
    formData.append('Password', $('#Password').val());


    $.ajax({
        type: 'POST',

        xhr: function() { // Custom XMLHttpRequest
            var myXhr = $.ajaxSettings.xhr();
            if (myXhr.upload) { // Check if upload property exists
                //myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
            }
            return myXhr;
        },
        cache: false,
        processData: false,
        contentType: false,
        data: formData,
        url: 'addAccounts.php',
        success: function(data) {
            console.log(data);
        },
        error: function(xhr, err) {
            console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
            console.log("responseText: " + xhr.responseText);
        }
    });

}

这是我发送表单的ajax代码。我在“addAccounts.php”页面中启用了error_reporting(E_ALL); ini_set('display_errors', '1');,只是为了查看错误。

我得到的第一个错误是$RemoveNoResponse = $_POST["RemoveNoResponse"];是无效索引。所以,为了仔细检查,我提出了$RemoveNoResponse = "0";,我又收到了一条错误,指出$minDistance = $_POST["minDistance"];是无效索引。

我继续测试它,我不相信错误在我的PHP中。

你们看到我的Ajax代码有什么问题吗?

这是一些HTML

    <input type="checkbox" id="RemoveNoResponse" name="RemoveNoResponse">
    <label for="RemoveNoResponse">Remove conversations you didn't get a Response Back from</label>

    <input type="text" name="minDistance" id="minDistance" value="0" />
    <i>Minimum distance between you and your match</i>
</div>

PHP的一部分

$percentageOfMessages = $_POST["percentageOfMessages"];
$RemoveDeletedAccounts = $_POST["RemoveDeletedAccounts"];
$RemoveNoReply = $_POST["RemoveNoReply"];
$RemoveNoResponse = $_POST["RemoveNoResponse"];
$minMatchPercent = $_POST["minMatchPercent"];
$minDistance = $_POST["minDistance"];
$maxDistance = $_POST["maxDistance"];

$blacklistUsernames = $_POST["blacklistUsernames"];
$pickuplineText = $_POST["pickuplineText"];
$userEmail = $_POST["userEmail"];
$Username = $_POST["Username"];
$Password = $_POST["Password"];

//$captcha = $_POST["captcha"];
//$num1 = $_POST["num1"];
//$num2 = $_POST["num2"];


if (empty($percentageOfMessages)) {
    echo "percentageOfMessages";

}elseif (empty($minMatchPercent)) {
    echo "minMatchPercent";

}elseif (empty($minDistance)) {
    echo "minDistance";

}elseif (empty($maxDistance)) {
    echo "maxDistance";

}elseif (empty($pickuplineText)) {
    echo "pickuplineText";

}elseif (empty($userEmail)) {
    echo "userEmail";

}elseif (empty($Username)) {
    echo "Username";

}elseif (empty($Password)) {
    echo "Password";

1 个答案:

答案 0 :(得分:1)

我不知道它是否是一个问题:但这似乎是错误的:

   if ($('RemoveNoResponse').prop('checked')) {
        formData.append('RemoveNoResponse', "1");
    } else {
        formData.append('RemoveNoReply', "0");
    }

应该是:

   if ($('RemoveNoResponse').prop('checked')) {
        formData.append('RemoveNoResponse', "1");
    } else {
        formData.append('RemoveNoResponse', "0");
    }

读取您的错误是&#34; $ RemoveNoResponse = $ _POST [&#34; RemoveNoResponse&#34;];&#34;是一个无效的索引,这是有道理的,因为这个错字 - $ _POST [&#34; RemoveNoResponse&#34;]不会是&#34; 0&#34;如果没有选中该复选框 - 在php中抛出错误,该错误预期该变量的值。