简体   繁体   English

存储在 PHP 变量中的 AJAX 响应的一部分

[英]A part of AJAX response storing in PHP variable

I need to store a piece of data, into PHP variable, which is received through AJAX response in an input box.我需要将一段数据存储到 PHP 变量中,该变量是通过输入框中的 AJAX 响应接收的。 How can I do this?我怎样才能做到这一点?

<script type="text/javascript">
  $(document).ready(function() {

    $("#user_id").change(function() {
      var id = $(this).val();
      var dataString = 'user_id='+ id;

      $.ajax({
        type: "POST",
        url: "wmat_details.php",
        data: dataString,
        cache: false,
        success: function(result) {
          var data = result.split(",");

          $('#name').val(data[0]);
          $('#email').val(data[1]);
          $('#ref_id').val(data[2]);
          $('#candidature_start').val(data[3]);
          $('#candidature_end').val(data[4]);
          $('#default_attempts').val(data[5]);
          $('#existing_complimentary').val(data[6]);
          $('#wmat_start').val(data[9]);
          $('#wmat_end').val(data[10]);
          $('#attempts_taken').val(data[11]);
        }
      });
    });
  });
</script>

As shown in above code, I want to store $('#attempts_taken').val(data[11]);如上面的代码所示,我想存储$('#attempts_taken').val(data[11]); this value to a PHP variable.这个值到一个 PHP 变量。 Any insight is appreciated.任何见解表示赞赏。

Unfortunately you can't.不幸的是你不能。

PHP is server side while jQuery (JS) is client side. PHP 是服务器端,而 jQuery (JS) 是客户端。 They are two separate layers of abstraction that interact only when the client call the server.它们是两个独立的抽象层,仅在客户端调用服务器时交互。

I don't have enough informations about what you need to do with data[11] but it seems that you have only one option: make a consecutive AJAX call to the php file that will manipulate data[11].我没有足够的信息来说明您需要对数据 [11] 做什么,但您似乎只有一种选择:对将操作数据 [11] 的 php 文件进行连续的 AJAX 调用。

The consecutive AJAX call must be executed from inside the first call success callback;连续的 AJAX 调用必须从第一个调用成功回调内部执行; something like this:像这样:

success: function(result){
     // Your on success logic
     // ...
     // Prepare the object to send to the server
     var objData = {};
     objData.attemptsTaken = data[11];
     // Execute the second AJAX call to the server
     $.ajax({
         type: "POST",
         url: "second_call_destination_file.php",
         data: objData,
         success: function(result){
             // Do something on success
         },
         error: function(){
             // Do something on error
         },
         complete: function(){
             // Do something on complete (executed after success and error)
         }
}

You cannot store ajax response into a php variable.您不能将 ajax 响应存储到 php 变量中。

way 1 :方式1:

You can make another ajax call.您可以进行另一个 ajax 调用。

way 2 :方式2:

you can set session.你可以设置会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM