Get print data json in real time

Asked

Viewed 692 times

0

Hello friends good night !

I have this code working perfectly I just need it to print the variable type every 5 seconds told me I had to do this with ajax more I do not know how to do this !

json

[ {"latitude": "-3.3462, -60.67900"}]

php

<?php
    // ler o json
    $file = file_get_contents('json.php');    

  
    $decode = json_decode($file, true);

    
    $ultimo = end($decode);

  ?>

<?php $variavel = $ultimo['latitude'];?>

1 answer

1

Yeah, you can work it out with Ajax / jQuery:

Home page (index php.):

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>
<body>
    <div id="DivSaida">
        <!-- VALOR DA VARIÁVEL SERÁ MOSTRADO AQUI -->
        <?php require_once("minhapagina.php"); ?>
    </div>
</body>
<script type="text/javascript">
    $(document).ready(function(){

        minhaUrl = "minhapagina.php"; // CAMINHO DA PÁGINA COM O ECHO

        setInterval(function(){

            $.ajax({
                url: minhaUrl,
                success: function( response ) {
                    $('#DivSaida').html( response );
                }
            });

        }, 5000); // TEMPO PARA ATUALIZAR EM MS (milissegundos)

    });
</script>
</html>

Page with the echo of the variable (php.):

<?php

$file = file_get_contents('json.php');
$decode = json_decode($file, true);
$ultimo = end($decode);
$variavel = $ultimo['latitude'];
echo $variavel; //MOSTRA EXATAMENTE ESSE VALOR NA PÁGINA PRINCIPAL

?>

I didn’t change your exit variable, just gave a echo! I left some comments in the code, which I found interesting you change.

  • Sorry I put it in here didn’t work What do you think I’m missing

  • I made a correction on home page. I developed within my own project and when posting, I put scr instead of src on the import of jQuery. Now it should work!

  • We can start a chat

  • I really need to finish this I hope you can help me thank you very much

  • as I do to enter

Browser other questions tagged

You are not signed in. Login or sign up in order to post.