0

这是arrivalPlay.php。如果用户单击来自arrivalRead.php 的数据并使url 变为arrivalPlay.php?id=1(2、3、4、5 等),则加载此页面。

<?php
    $con = mysqli_connect("localhost","admin","admin","flight_status");
    $id = $_GET['id'];
    $getrow = mysqli_query($con, "SELECT * FROM arrival WHERE id='$id'");
    $row = mysqli_fetch_array($getrow);
    mysqli_close($con); 
    $order = array(1,2,3,4);
    foreach ($order as $o) {
        $res[$o][f] = $row[$o];
    }
    json_encode($res);
?>

这是 getData.js 文件。文件文件接收 res 并将传递给“mp”。

<script>
    function aha() {
    $.ajax({
        url:'arrivalPlay.php',
        data:{id:3},
        dataType:'json',
        type:'GET',
        success:function(data){
            document.write(data[1].f);
            document.write(data[2].f);
            document.write(data[3].f);
            document.write(data[4].f);
        }
    });
    }
</script>

页面到达Play.php 仅在 url 变为到达播放.php?id=X 时才有数据。有什么方法可以将数据从“动态”php 检索到 javascript 页面?如果您认为这很奇怪,请随时更改我的方法。谢谢...

4

2 回答 2

1

试试这个:

首先在你的服务器页面申请echo之前json_encode($res);

它应该是echo json_encode($res);

然后如果它不起作用,那么试试这个代码

<script>
$(document).ready(function(){
    $(document).on('click','#a',function(e){
        e.preventDefault();
        $.ajax({
            url:'arrivalPlay.php',
            data:{id:1},
            dataType:'json',
            success:function(data){
                $('#res').html(data);
            }
        });
    });
});
</script>

如果你想json从服务器然后只json data应该从server

就像在你的代码中

<?php
    $con = mysqli_connect("localhost","admin","admin","flight_status");
    $id = $_GET['id'];
    $getrow = mysqli_query($con, "SELECT * FROM arrival WHERE id='$id'");
    $row = mysqli_fetch_array($getrow);
    mysqli_close($con); 
    $res=array();
    $order = array('airline','flight','origin','status');
    foreach ($order as $o) {
        $res[$o] = $row[$o];
    }
    echo json_encode($res);// echo the json string
    // remember that no other output should be generated other than this json
    return; //so you can use this line
?>

足够

你不想json然后你使用这个代码

echo implode(',',$res); 代替 echo json_encode($res);

在这种情况下也javascript删除此选项dataType:'json',

阅读jquery.ajax

于 2013-05-02T07:43:44.573 回答
0

由于您正在接收 JSON 数据,我怀疑您是否愿意将它们放入 HTML 元素中。我要么更改我的 PHP 文件以生成 HTML 元素,要么实现 som javascript 逻辑以基于服务器提供的 JSON 数据创建元素。

于 2013-05-02T07:54:08.067 回答