简体   繁体   中英

Retrieve data from dynamic php page to javascript using jquery

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

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

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

Try this:

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

It should be 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

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

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

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

Read jquery.ajax

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM