如何在JavaScript中获取Ajax JSON循环响应

时间:2020-02-10 15:30:53

标签: php json ajax loops

我正在使用ajax调用将值发送到php文件,并将响应返回到第一页(工作正常),但是当我以循环形式获取结果时,该响应将不会在第一页上返回。

我的第一页:

$.ajax({
    url: "action.php",
    method: "post",
    data: {
        'trader': selected_trader
    },
    dataType: 'JSON',
    success: function (response) {
        $('#date').html(response['date']);
        $('#symbol').html(response['symbol']);
        $('#status').html(response['status']);
        $('#alert').html(response['alert']);
        $('#company_name').html(response['company_name']);
    }
})

我的第二页:

if (isset($_POST['trader'])) {
    $trader = $_POST['trader'];

    $sql = "Select * from current_trader where status='$trader' ";
    $result = mysqli_query($conn, $sql);
    while ($row = mysqli_fetch_assoc($result)) {
        $data["date"] = date('Y:m:d', strtotime($row['date']));
        $data["symbol"] = $row['symbol'];
        $data["company_name"] = $row['company_name'];
        $data["alert"] = $row['alert'];
        $data["status"] = $row['status'];
        echo json_encode($data);
    }
}

0 个答案:

没有答案