如何在ajax响应中使用while循环

时间:2014-05-08 02:28:20

标签: php jquery ajax

这是我的ajax

var vn = '*', dataString = 'vehicle_name='+ vn;

  $.ajax({
        type: "POST",
        url: "getDesc.php",
        data: dataString,
        dataType: "json",
        success: function(data){
            if(!data.error && data.success) {
             var vn = data.vehicleName, st = data.serviceType, o = data.others, rn = data.ref_no, rd = data.ref_date, rt = data.ref_type;
             var myTable1 ='<fieldset>' +
                 '<legend>Dashboard</legend><br>' +
                    '<table border="1" width="650">' +
                    '<tr bgcolor=#c0c0c0>' +
                            '<td width=100 align="center"><font face="helvetica"><b>Vehicle Name</b></font></td>' +
                            '<td width=80 align="center"><font face="helvetica"><b>Service Type</b></font></td>' +
                            '<td width=80 align="center"><font face="helvetica"><b>Others</b></font></td>' +
                            '<td width=100 align="center"><font face="helvetica"><b>Reference No.</b></font></td>' +
                            '<td width=80 align="center"><font face="helvetica"><b>Reference Date</b></font></td>' +
                            '<td width=80 align="center"><font face="helvetica"><b>Reference Type</b></font></td>' +
                        '</tr>' +

                        '<tr bgcolor=#c0c0c0>' +
                            '<td align=center>'+vn+'</td>' +
                            '<td align=center>'+st+'</td>' +
                            '<td align=center>'+o+'</td>' +
                            '<td align=center>'+rn+'</td>' +
                            '<td align=center>'+rd+'</td>' +
                            '<td align=center>'+rt+'</td>' +
                        '</tr>' +
                    '</table>' +
                 '</fieldset>';
                  $('#tabs-4').append(myTable1);

            } else {
                alert(data.errorMsg);
            }
        }
        });

这是我的PHP

<?PHP
include("db.classes.php");
$g = new DB();
$g->connection();

        if($_POST)
            {
                $vehicleName = $g->clean($_POST["vehicle_name"],1);

                $g->getAllDesc($vehicleName);
            }
$g->close();
 ?>

这是我的db.classes

public function getAllDesc($vehicleName)
    {
        header('Content-type: application/json');
        $sql = "select vehicle_name, service_type, others, ref_no, ref_date, ref_type from vehicle_services";

        $result = mysql_query($sql) or die(json_encode(array('error' => 0,'errorMsg' => "MySQL query failed.")));

        while($row = mysql_fetch_array($result))
        {

            $output[] = array(
                'success' => 1,
                'vehicleName' => $row["vehicle_name"],
                'serviceType' => $row["service_type"],
                'others' => $row["others"],
                'ref_no' => $row["ref_no"],
                'ref_date' => $row["ref_date"],
                'ref_type' => $row["ref_type"]                 
                );

                echo json_encode($output);
        }

    }

这是我得到的回应

  

[{&#34;成功&#34;:1,&#34; vehicleName&#34;:&#34; JHE699&#34;&#34;的serviceType&#34;:&#34; CHANGE   TIRE&#34;&#34;其他&#34;:&#34;&#34;&#34; ref_no&#34;:&#34; 14010001&#34;&#34; ref_date&#34 ;: &#34; 2014年4月10日&#34;&#34; ref_type&#34;:&#34; PRRIAR&#34;}] [{&#34;成功&#34;:1,&#34; vehicleName& #34;:&#34; JHE699&#34;&#34;的serviceType&#34;:&#34; CHANGE   TIRE&#34;&#34;其他&#34;:&#34;&#34;&#34; ref_no&#34;:&#34; 14010001&#34;&#34; ref_date&#34 ;: &#34; 2014年4月10日&#34;&#34; ref_type&#34;:&#34; PRRIAR&#34;},{&#34;成功&#34;:1,&#34; vehicleName&# 34;:&#34; TOYOTA   86&#34;&#34;的serviceType&#34;:&#34;卡车&#34;&#34;其他&#34;:&#34;&#34;&#34; ref_no&#34 ;: &#34; 1&#34;&#34; ref_date&#34;:&#34; 2014年5月7日&#34;&#34; ref_type&#34;:&#34; RR&#34;}]

我的第一行重复,表格不显示。如何传递数组以便显示我的表?

1 个答案:

答案 0 :(得分:0)

您没有获得打印表的原因是您的JSON无效。

JSON响应通常是一个JSON对象,但在您的情况下,您的while循环发生3次,并且您获得3个JSON对象。你的jQuery不喜欢响应,所以会出错。

我建议您在while循环($output)上正确实例化$output = array()数组,然后,在while循环之后返回整个$output数组,而不是直接回显数组(return $output)。然后你可以使用:

$array = $g->getAllDesc($vehicleName); 
echo json_encode($array);. 

您可能想要检查您的数据库是否没有同一车辆的双倍。