使用PHP将数组传递给Ajax

时间:2022-05-27 02:09:39

I want to pass an array to my Ajax request so I can update multiple elements with the same query. Here is what I do:

我想将一个数组传递给我的Ajax请求,以便我可以使用相同的查询更新多个元素。这是我做的:

My Ajax call:

我的Ajax电话:

$("body").on("focusout", "input", function () {
    var id = ($(this).attr("id"));
    var container = '#' + id + "_ck";
    var data_type = ($(this).attr("data-type"));
    var text = document.getElementById(id).value;
    $.ajax({
        url: 'ajax-php.php',
        type: 'post',
        data: { 'action': 'input_ck', 'id': id, 'text': text, 'data_type': data_type },
        success: function (data, status) {
            $(container).html(data); <-------------------------------- PHP RESPONSE
        },
        error: function (xhr, desc, err) {
            console.log(xhr);
            console.log("Details: " + desc + "\nError:" + err);
        }
    });
});  

Now I know that I can return an array so that the data contain my array in JSON.

现在我知道我可以返回一个数组,以便数据包含我在JSON中的数组。

Let's say that PHP returns this:

假设PHP返回:

$arr($var1,$var2);
echo json_encode($arr);

Could I do something like this back in my jQuery Ajax script?

我可以在我的jQuery Ajax脚本中做这样的事情吗?

foreach(data as value){
   i=0;
   $(container[i]).html(data[i]);
   i++;
}

I would also make an array out of containers. I'm not sure about the syntax here but would it be possible?

我也会用容器制作一个数组。我不确定这里的语法,但它可能吗?

2 个解决方案

#1


2  

Yes of course you can, but you need to tweak around ajax properties to meet the expectation output of response like following :

是的,你当然可以,但你需要调整ajax属性以满足响应的期望输出,如下所示:

$.ajax({
    url: 'ajax-php.php',
    type: 'post',
    dataType : 'json', //<----- add here
    data: { 'action': 'input_ck', 'id': id, 'text': text, 'data_type': data_type },
    success: function (data, status) {
       // $(container).html(data); <-------------------------------- PHP RESPONSE
      // Add here
      //i=0; 
      // you can either use $.each or forEach or native for loops here
      $.each(data, function(i,e){ //< i = index, e = element
         // i'm not sure with this selector
         // you need to show what kind of container element did you have           
         $(container[i]).html(data[i]);// or easier if using e.property_name
         //i++;
      }
    },
    error: function (xhr, desc, err) {
        console.log(xhr);
        console.log("Details: " + desc + "\nError:" + err);
    }
});

#2


1  

PHP

$json = array('vals' => array($var1, $var2));
header('Content-Type: application/json');
echo json_encode($json)

Jquery

if(data.vals){
  $.each(data.vals, function(key, value) {
        $(container[key]).html(value);
  });
}

Untested

#1


2  

Yes of course you can, but you need to tweak around ajax properties to meet the expectation output of response like following :

是的,你当然可以,但你需要调整ajax属性以满足响应的期望输出,如下所示:

$.ajax({
    url: 'ajax-php.php',
    type: 'post',
    dataType : 'json', //<----- add here
    data: { 'action': 'input_ck', 'id': id, 'text': text, 'data_type': data_type },
    success: function (data, status) {
       // $(container).html(data); <-------------------------------- PHP RESPONSE
      // Add here
      //i=0; 
      // you can either use $.each or forEach or native for loops here
      $.each(data, function(i,e){ //< i = index, e = element
         // i'm not sure with this selector
         // you need to show what kind of container element did you have           
         $(container[i]).html(data[i]);// or easier if using e.property_name
         //i++;
      }
    },
    error: function (xhr, desc, err) {
        console.log(xhr);
        console.log("Details: " + desc + "\nError:" + err);
    }
});

#2


1  

PHP

$json = array('vals' => array($var1, $var2));
header('Content-Type: application/json');
echo json_encode($json)

Jquery

if(data.vals){
  $.each(data.vals, function(key, value) {
        $(container[key]).html(value);
  });
}

Untested