【问题标题】:How to pass ajax post response to PHP如何将ajax发布响应传递给PHP
【发布时间】:2017-08-09 20:14:30
【问题描述】:

我有一个页面 index.php -> php 代码、jquery 代码和 php 代码合二为一。

$.ajax({
                url: url,
                method: "post",
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(inputData),
                success: function(data, status, xhr) {
                    if (data.result) {

                     // I want to store this data.result and pass it to PHP code below.
                       $("#some-form").submit();
                    } else {
                    // some code here
                    }
                },
                error: function(data, status, xhr) {
                    // Show a modal informing user of invalid 
                       information

                }
})

我在 data.result 的成功块中看到正确的数据。 我想将此值传递给下面的 php 代码:

<?php
  function buildRedirectUrl($url) {


    return $url .
                 "&email=" . trim($_POST["email"]) .                     
                 "&state=" . trim($_POST["state"]) .                   
                 "&application_code=" . 
              trim($_POST["application_code"]);


     // I want to add the data.result from success block here
     // something like :
     //   "&result=" . trim($_POST["data.result"]) . 

   }

    $redirectUrl = buildRedirectUrl($someUrl);
    header('Location: ' . $redirectUrl);
 ?>

有什么解决办法吗? 是否可以将 ajax 调用中的数据传递给 php? 任何示例都会有所帮助。我找到了所有访问 inputParameters 的示例,但我没有看到任何关于如何在 php 中使用响应的示例

【问题讨论】:

  • 我猜如果你删除JSON.stringify 如果工作正常?再说一次,我不知道你想做什么?
  • 我能够成功发出 POST 请求。我在成功块中看到了正确的结果。我想存储响应 data.result 并在我的 PHP 代码中访问它。
  • 您为什么要这样做?在提交之前,您需要将结果存储在 #some-form 中。为什么你首先要在 ajax 中做任何事情。只需使用 inputData 提交表单。
  • 我想使用 header() 在另一个 html 页面中自动填充该字段...
  • 使用具有这种模式的现有代码库:(

标签: php jquery ajax post


【解决方案1】:

注意:此代码功能不完整,只是一个概念。

我想如果你必须这样做,你可以做这样的事情;

$.ajax({
  url: url,
  method: "post",
  contentType: "application/json",
  dataType: "json",
  data: JSON.stringify(inputData),
  success: function(data, status, xhr) {
    if (data.result) {
      //following 3 lines are new.
      var elem = $("<input type='hidden' name='ajaxResult' />");
      elem.val(data.result);
      $("#some-form").append(elem);
      // I want to store this data.result and pass it to PHP code below.
      $("#some-form").submit();
    } else {
      // some code here
    }
  },
  error: function(data, status, xhr) {
    // Show a modal informing user of invalid 
    information

  }
})
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

然后你可以在你的表单帖子$POST["ajaxResult"]中使用结果

【讨论】:

  • 它没有给出任何错误但它给出了一个空白值
  • 成功了。我在代码中打错了字。非常感谢。感谢您的帮助!
【解决方案2】:

您可以像下面这样动态创建一个表单并提交它

$.ajax({
        url: url,
        method: "post",
        contentType: "application/json",
        dataType: "json",
        data: JSON.stringify(inputData),
        success: function(data, status, xhr) {
            if (data.result) {
                var method = "post";
                var action = "site.com/index.php";
                var form = document.createElement("form");
                form.setAttribute("method", method);
                form.setAttribute("action", action);
                var hiddenField = document.createElement("input");
                hiddenField.setAttribute("type", "hidden");
                hiddenField.setAttribute("name", "field_name");
                hiddenField.setAttribute("value", data.result);
                form.appendChild(hiddenField);
                document.body.appendChild(form);
                form.submit();

            } else {
            // some code here
            }
        },
        error: function(data, status, xhr) {
            // Show a modal informing user of invalid 
               information

        }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    相关资源
    最近更新 更多