使用jquery从动态php页面检索数据到javascript

时间:2022-12-05 11:53:55

This is arrivalPlay.php. This page is loaded if user click data from arrivalRead.php and make the url become arrivalPlay.php?id=1 (2,3,4,5 and so on).

这是arrivalPlay.php。如果用户单击arrivalRead的数据,将加载此页面。让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);
?>

This is getData.js file. The file file receive res and will be passed to 'mp'.

这是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>

Page arrivalPlay.php only has data if the url become arrivalPlay.php?id=X. Is there any way to retrieve data from the 'dynamic' php to the javascript page? Feel free to change my approach if you think it is odd. Thank you...

arrivalPlay页。php只有在url变为arrivalPlay.php?id=X时才具有数据。有没有办法从“动态”php中检索数据到javascript页面?如果你认为这很奇怪的话,请随意改变我的做法。谢谢你!

2 个解决方案

#1


0  

Try this:

试试这个:

First in your server page apply echo before json_encode($res);

首先在您的服务器页面上应用echo之前的json_encode($res);

It should be echo json_encode($res);

它应该是echo json_encode($res);

And then if it not works then try this code

如果它不工作,那么试试这个代码

<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>

If you want json from server then only json data should be passed from server

如果您希望从服务器获得json,那么只能从服务器传递json数据。

like in your code

就像在你的代码

<?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
?>

is enough

就足够了

but you don't want json then you use this code

但你不想要json,那你就用这段代码

echo implode(',',$res); instead of echo json_encode($res);

回声内爆(',',res美元);而不是回声json_encode(res);

also in javascript remove this option dataType:'json', in this case.

在javascript中,也可以删除这个选项数据类型:“json”。

Read jquery.ajax

读jquery.ajax

#2


0  

Since you are receiving JSON data, I doubt that you would like to place them into an HTML element. I would either change my PHP file to produce HTML elements, or implement som javascript logic to create elements based on the JSON data the server provides.

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

#1


0  

Try this:

试试这个:

First in your server page apply echo before json_encode($res);

首先在您的服务器页面上应用echo之前的json_encode($res);

It should be echo json_encode($res);

它应该是echo json_encode($res);

And then if it not works then try this code

如果它不工作,那么试试这个代码

<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>

If you want json from server then only json data should be passed from server

如果您希望从服务器获得json,那么只能从服务器传递json数据。

like in your code

就像在你的代码

<?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
?>

is enough

就足够了

but you don't want json then you use this code

但你不想要json,那你就用这段代码

echo implode(',',$res); instead of echo json_encode($res);

回声内爆(',',res美元);而不是回声json_encode(res);

also in javascript remove this option dataType:'json', in this case.

在javascript中,也可以删除这个选项数据类型:“json”。

Read jquery.ajax

读jquery.ajax

#2


0  

Since you are receiving JSON data, I doubt that you would like to place them into an HTML element. I would either change my PHP file to produce HTML elements, or implement som javascript logic to create elements based on the JSON data the server provides.

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