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

Answers


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.


Need Your Help

Intranet Uptime Monitoring Component

iis intranet uptime

I have a MOSS 2007 test site, its not public facing, instead its on our intranet, I am looking for an uptime monitoring component thats free and easy to install, any suggestions?

getting multiple post id wordpress

php jquery wordpress

I am making a voting system for my theme

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.