gpt4 book ai didi

javascript - 我的 AJAX 表单未发送到我的 PHP

转载 作者:行者123 更新时间:2023-12-03 07:20:52 26 4
gpt4 key购买 nike

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 代码。我启用了error_reporting(E_ALL);
ini_set('display_errors', '1');
在我的“addAccounts.php”页面中只是为了看看错误是什么。

我得到的第一个错误是 $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";

最佳答案

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

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

得知您的错误是“$RemoveNoResponse = $_POST["RemoveNoResponse"];”是一个无效的索引,这是有道理的,因为由于这个拼写错误 - $_POST["RemoveNoResponse"] 如果未选中该复选框,则不会为“0” - 在需要该变量值的 php 中抛出错误。

关于javascript - 我的 AJAX 表单未发送到我的 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36217404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com