weixin_33734785 2016-03-10 13:07 采纳率: 0%
浏览 17

如何更新实时数据?

I'm trying to figure out how to update my live data, I found some examples on Google of Ajax but I can't seem to get them to work.

The part that contains and places the live data in a paragraph is :

$file = "Data.txt";
$data = file($file);
$line = $data[count($data)-1];

for($i=1;$i<6;$i++){
    switch ($line) {
    case $i:
    echo "<p class ='bus".$i."'> <img id='bus' src = 'bus.png'> </p>";
    break;
    }
}

This is the full html file

<!DOCTYPE>
<html>
     <head>
        <title>Bus</title>  
        <link rel="stylesheet" href="stijlenbestand.css">
    </head>
    <body>  
        <?php
            //aanmaken 5 bushaltes
            echo '<figure>';    
            for($i=1;$i<6;$i++){
                echo "<img src = 'bushalte.png'>";
            }   
            echo '</figure>';

            //laatste lijn van tekstbestand.
            $file = "Data.txt";
            $data = file($file);
            $line = $data[count($data)-1];

            for($i=1;$i<6;$i++){
                switch ($line) {
                case $i:
                echo "<p class ='bus".$i."'> <img id='bus' src = 'bus.png'> </p>";
                break;
                }
            }           
        ?>          
    </body>
</html>
  • 写回答

2条回答 默认 最新

  • weixin_33739541 2016-03-10 13:15
    关注

    For a live update you need two parts.

    First is the part where your page is and the second part is where your data comes from.

    Php is a very static language. Once your script is finished it won't do anything anymore.

    For a "live-website" you need Javascript.

    if you want to use jQuery i would recommend you to use the jQuery.post() function.

    jQuery Code in your Website:

    $.post( "test.php", { name: "John", time: "2pm" })
      .done(function( data ) {
        alert( "Data Loaded: " + data );
      });
    

    Your test.php

    if(isset($_POST['name'])) {
      //Do Some Stuff
      $a = 'var a';
    
      echo json_encode($a);
    }
    
    评论

报告相同问题?