如何将ajax post响应传递给PHP

时间:2017-08-09 20:14:31

标签: php jquery ajax post

我有一个页面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中使用响应的示例

2 个答案:

答案 0 :(得分:0)

您可以动态创建表单并按如下所示提交

$.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

        }
})

答案 1 :(得分:0)

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

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

$.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

  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

然后,您可以在表单$POST["ajaxResult"]

中使用结果