【问题标题】:Ajax POST, check PHP $_GET and then $_POSTAjax POST,检查 PHP $_GET 然后 $_POST
【发布时间】: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 逻辑错误,如果有人能指导我正确的方向,将不胜感激。

【问题讨论】:

    标签: php ajax post get


    【解决方案1】:
    url: window.location.pathname,
    

    您的 Ajax 永远不会将数据发布到带有查询字符串的 URL,因为您明确地采用了路径名。

    也许你想要location.href

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 2012-01-14
      • 1970-01-01
      • 2014-12-12
      • 2021-02-21
      • 1970-01-01
      相关资源
      最近更新 更多