【发布时间】:2021-10-15 20:05:11
【问题描述】:
在表单提交时使用以下 Ajax POST 函数(此处简化):
$form.on("submit", function (i) {
i.preventDefault();
var sendEmail = 1;
ValidateForm(sendEmail, "goSignup");
});
function ValidateForm(sendEmail, action) {
$.ajax({
type: "POST",
url: window.location.pathname,
dataType: "json",
data: {
ajaxRequest: 1,
sendEmail: sendEmail,
}
}
在我发布这个之后,我想使用一个等于 1 的条件 GET 参数(即https://www.example.com?test-parameter=1),然后如果它存在于 URL 中,如果从我的 PHP 中的 $_POST 接收到 ajaxRequest,则使用那里的一个或另一个函数:
public function __construct() {
$testingParameter = $_GET["test-parameter"] ?? '';
if (trim($testingParameter) == '1') { // if has get parameter equal
if (!empty($_POST['ajaxRequest'])) { // if JS postRequest has been posted
$this->handlePostRequests();
}
echo 'has get parameter';
} else { // else use old logic
if (!empty($_POST['ajaxRequest'])) {
$this->handleOtherRequests();
}
echo 'no get parameter';
}
}
问题:
我从 PHP 中得到了正确的回显,但是当我使用 Ajax 提交表单时,如果我使用的是 url www.example.com?test-parameter=1,它仍然使用 handleOtherRequests(); 而不是 handlePostRequests(); 函数。
这里可能会出现一些基本的 PHP 逻辑错误,如果有人能指导我正确的方向,将不胜感激。
【问题讨论】: