如何在php表中打印多个ajax响应值

时间:2014-09-18 07:23:45

标签: php jquery mysql ajax

我有一个ajax响应问题,打印一个表。我这样的ajax代码

$(document).ready(function(){

var terminalid=$('#terminal_id').val();
var pin=$('#pin').val();
var agentid=$('#agentid').val();

var agenterminal=terminalid.split(",");
var agent_id=agentid.split(",");
var agentaccountpin=pin.split(",");

var agr=[];
var pin_arr=[];
var ter_arr=[];

for (var i=0; i<agent_id.length; i++)
    agr.push( + agent_id[i] );

for (var j=0; j<agentaccountpin.length; j++)
    pin_arr.push( + agentaccountpin[j] );

for (var k=0; k<agenterminal.length; k++)
    ter_arr.push( + agenterminal[k] );

var final_arr=[];
for(var i=0; i<agr.length; i++)
{
    //alert(agr[i]);
    var datastring="terminal_id="+ter_arr[i]+"pin="+pin_arr[i]+"agentid="+agr[i];
    var geturl="myurl";
    $.ajax({
        type:"POST",
        url:geturl,
        data:datastring,
        success: function(resp){
            alert(resp);
            $("#balance").html(resp);
        }

     });
}

});

这是我的PHP代码:

 foreach ($this->agent_summary_results as $row)
 {
        // Some Code for Data printing in table format
        <tr>
            <td class="Row" width="12%" valign="top" >name</td>
            <td valign=top width="12%" class="Row">credit bal</td>
            <td>
              <div id="balance"><FONT COLOR=RED size=3>Calculating...</FONT></div>
            </td>
        </tr>
$terminalid[]=$terminal_id;
      $account_card_pin[]=$pin;
      $id_agent[]=$agentid;
    }
$agentterminalid=implode(",", $terminalid);
  $agentaccountpin=implode(",",$account_card_pin);
  $agent_id=implode(",",$id_agent);
<input type="hidden" name="terminal_id" id="terminal_id" value="<?php echo $agentterminalid;?>">
  <input type="hidden" name="pin" id="pin" value="<?php echo $agentaccountpin;?>">
  <input type="hidden" name="agentid" id="agentid" value="<?php echo $agent_id;?>">

例如我的o / p如下所示 S.no name balance

1 tgest 12.34
2测试计算..
3沙计算..

像这样我得到了输出。在ajax我收到所有回复。但是在表格中没有显示。请帮我。怎么解决这个问题。感谢

响应仅在第一行打印。但我有总数。

1 个答案:

答案 0 :(得分:0)

您应该在循环中构建一个javascript数组,然后使用此数组发送单个ajax请求data

var dataArray = [];

for(var i=0; i<agr.length; i++)
{
    dataArray.push({
        terminal_id: ter_arr[i],
        pin: pin_arr[i],
        agentid: agr[i]
    });
}

$.ajax({
    type: "POST",
    url: "myurl",
    data: {
        mydata: dataArray // You will have a "$_POST['mydata']" variable in PHP
    }, 
    success: function(resp){
        $("#balance").html(resp);
    }
});