简体   繁体   中英

My AJAX form isn't sending to my PHP

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);
        }
    });

}

This is the ajax code I have for sending a form. I enabled error_reporting(E_ALL); ini_set('display_errors', '1'); error_reporting(E_ALL); ini_set('display_errors', '1'); in my 'addAccounts.php' page just to see what the errors are.

The first error I got was that $RemoveNoResponse = $_POST["RemoveNoResponse"]; was an invalid index. So just to double check, I put $RemoveNoResponse = "0"; and I got another error saying $minDistance = $_POST["minDistance"]; was an invalid index.

I went ahead and tested it and I don't believe the error is in my PHP.

Do you guys see anything wrong with my Ajax code?

Here is a bit of the 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>

And part of the 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";

I don't know if its an issue: but this seems wrong:

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

should it be :

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

reading that your error was "$RemoveNoResponse = $_POST["RemoveNoResponse"];" was an invalid index, this would make sense in that due to this typo - $_POST["RemoveNoResponse"] would not have been "0" if that checkbox was not checked - throwing the error in the php which expected a value for that variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM