数据未在while循环内显示

时间:2018-10-23 15:29:08

标签: javascript php

当我直接访问php文件时,将显示数据,但是当将数据传递回ajax时,将不显示循环内的数据。

showpercenage.php

<?php
$get_id = $_POST;
$landing_id = implode(" ",$get_id);

$query = mysqli_query($conn, "SELECT
              percentage.percent_id,
              percentage.landing_id,
              percentage.percentage,
              percentage.date_recorded
              FROM
              percentage
              WHERE percentage.landing_id = '$landing_id'");`
$number = 1;
  while($row = mysqli_fetch_array($query)) {

    $data .= '
      <tr>
        <td><center>' . $number . '</center></td>
        <td><center>' . $row['percent_id'] . '</center></td>
        <td><center>' . $row['landing_id'] . '</center></td>
        <td><center>' . $row['percentage'] . '%</center></td>
        <td><center>' . date("M. d, Y", strtotime($row['date_recorded'])) . '</center></td>
      </tr>';
    $number++;
  }

echo $data;
?>

js文件

function readRecords() {
  $.get("ajax/showpercent.php", {}, function (data, status) {
    $(".display_percentage").html(data);
  });
}

任何人都可以帮助我解决此问题。

1 个答案:

答案 0 :(得分:0)

只需在while循环之前定义$ data变量即可。

$number = 1;
$data = '';
while($row = mysqli_fetch_array($query)) {

        $data .= '
          <tr>
            <td><center>' . $number . '</center></td>
            <td><center>' . $row['percent_id'] . '</center></td>
            <td><center>' . $row['landing_id'] . '</center></td>
            <td><center>' . $row['percentage'] . '%</center></td>
            <td><center>' . date("M. d, Y", strtotime($row['date_recorded'])) . '</center></td>
          </tr>';
        $number++;
}

echo $data;