简体   繁体   English

使用jQuery ajax POST接收多个数据

[英]Receiving multiple data with jQuery ajax POST

I am using ajax to get the data and populate a select box. 我正在使用ajax获取数据并填充一个选择框。 In the following code, I echo the output on the ajax.php file, and receive it. 在以下代码中,我在ajax.php文件上回显输出,并将其接收。 Everything works fine. 一切正常。 However, as you can see that all of the html is populated in one select box. 但是,如您所见,所有html都填充在一个选择框中。 I want to return data of two separate select boxes instead of one. 我想返回两个单独的选择框而不是一个的数据。

Therefore my question is, how can I send two different type of data separately from ajax.php and how can I receieve it on success function. 因此,我的问题是,如何从ajax.php分别发送两种不同类型的数据,以及如何在成功函数中接收它。

$.ajax({
    type: "POST",
    url: "../ajax.php",
    data: {name: 'select1', id: id},
    cache: false,
    success: function(html){                
        $(".select1").html(html);
    }           
});

Ajax.php only echos the data: Ajax.php仅回显数据:

//data for select1
foreach($rows as $row){ 
    echo '<option>x</option>';      
}

//I want to add another one for select2
foreach($rows1 as $row){    
    echo '<option>y</option>';      
}       
die();

and want to receive with something like: 并希望收到以下内容:

success: function(html){                
    $(".select1").html(data1);
    $(".select2").html(data2);
}

Edit: I'm fetching the data using the ajax because I'm using dependent select boxes and fetching the data from the database. 编辑:我正在使用ajax来获取数据,因为我正在使用从属选择框并从数据库中获取数据。 So by clicking at select 1, it fetches the data for the select 2 from database, that's why I'm doing that. 因此,通过单击select 1,它将从数据库中获取select 2的数据,这就是我这样做的原因。

you could do some thing like this in your Ajax.php 您可以在Ajax.php中做类似的事情

//select1
$data['html1'] = NULL;
foreach($rows as $row){ 
    $data['html1'] .= '<option>x</option>';      
}
//select2
$data['html2'] = NULL;
foreach($rows1 as $row){    
   $data['html1'] .= '<option>y</option>';      
}  
$resonse = json_encode($data);
echo $response;
//JQUERY MAKE SURE THAT ITS OF TYPE JSON.
success: function(response){                
    $(".select1").html(response.html1);
    $(".select2").html(response.html2);
}

construct two diffrent select options in php with two different array indexes like this 用两个不同的数组索引two diffrent select options in php构造two diffrent select options in php

//select1
    $data['first'] = '<option>first</option>';   // loop for more data    
//select2
   $data['second'] = '<option>second</option>';      

echo json_encode($data); 回声json_encode($data); and recieve it in success and show accordingly. 并成功地收到并相应地展示。

success: function(data){
  var data = JSON.parse(data);
  $(".select1").html(data.first);
  $(".select2").html(data.second);
}
foreach($rows as $row){ 
    $selects['select1'] = '<option>x</option>';      
}

foreach($rows1 as $row){    
    $selects['select2'] = '<option>y</option>';      
}    

print_r(json_encode($selects));

die();

$.ajax({
    type: "POST",
    url: "../ajax.php",
    success: function(data){          
        $(".select1").html(data.select1);
        $(".select2").html(data.select2);
    }           
});

To separate the data that is returned, you are going to want to use a different format to return the data - at the moment, all it returns is 为了分隔返回的数据,您将要使用其他格式来返回数据-目前,返回的所有内容是

<option>x</option>
<option>x</option>
<option>x</option>
<option>y</option>
<option>y</option>
<option>y</option>

which cannot be separated by the browser. 不能由浏览器分隔。 If you encode it as JSON, you can make it return 如果将其编码为JSON,则可以使其返回

{
  select1: [
    "<option>x</option>",
    "<option>x</option>",
    "<option>x</option>"
  ]
  select2: 
    "<option>y</option>",
    "<option>y</option>",
    "<option>y</option>",
  ]
}

To do this, you need to use this php: 为此,您需要使用以下php:

$select1 = array();
foreach($rows1 as $row){
  array_push($select1, $row);
}
$select2 = array();
foreach($rows2 as $row){
  array_push($select2, $row);
}
echo json_encode(array('select1' => $select1, 'select2' => $select2), JSON_FORCE_OBJECT);

and this javascript: 和这个JavaScript:

success: function(data){
  var decoded = JSON.parse(data);
  for (var l = 0; l < decoded.select1.length; l++){
    $(".select1").append(decoded.select1[l]);
  }
  for (var l = 0; l < decoded.select2.length; l++){
    $(".select2").append(decoded.select2[l]);
  }
}
var json_post = [ 
    { "id" : "1", "name" : "test1" },
    { "id" : "2", "name" : "test2" }
];

$.ajax({
    type: "POST",
    url: "../ajax.php",
    data: json_post,
    cache: false,
    dataType: 'json',
    success: function(data){                
        var json = $.parseJSON(data);
        $.each(
            json,
            function(i, val){
                $(".select"+val.id).html(val.name)
            }
        );
    }
});

You could do something along the above lines with json. 您可以使用json沿上述方式进行操作。 (I haven't tested it). (我还没有测试过)。

Json is the best choice. 杰森是最好的选择。 even if you don't want to use that, then another option is here. 即使您不想使用它,这里也有另一个选择。

//select1
$data = "";
foreach($rows as $row){ 
    $data .= '<option>x</option>';      
}
$data .= "^^^"; // you can use any separator
//select2

foreach($rows1 as $row){    
   $data .= '<option>y</option>';      
}  

echo $data;

success: function(response){
    var options = response.split("^^^");

    $(".select1").html(options[0]);
    $(".select2").html(options[1]);
}

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

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