Php 从外部网页获取并显示常量,并在更改时更新

Php 从外部网页获取并显示常量,并在更改时更新,php,ajax,external,auto-update,ajaxform,Php,Ajax,External,Auto Update,Ajaxform,此脚本从www.time.is和#twd ID接收时间,并在单击发送btn时显示 我有两个问题: 1-仅显示首次接收时间,再次单击发送时不更新 2-页面刷新和丢失数据 我对以下代码有三个问题: <?php include 'simple_html_dom.php'; ?> <!DOCTYPE html> <html> <head> <title>Untitled 1</title>

此脚本从www.time.is和#twd ID接收时间,并在单击发送btn时显示

我有两个问题:

1-仅显示首次接收时间,再次单击发送时不更新

2-页面刷新和丢失数据

我对以下代码有三个问题:

<?php include 'simple_html_dom.php'; ?>
<!DOCTYPE html>
<html>

    <head>
        <title>Untitled 1</title>
        <script src="js/jquery.js"></script>
    </head>

    <body>

        <form action="" method="POST">
            <input id="url" name="url" type="text" value="http://www.time.is/">
            <input id="address" name="address" type="text" value="#twd">
            <input id="send" type="submit" value="get">
        </form>

        <ul id="times">

        </ul>

        <script type="text/javascript">
            $("#send").click(function(events) {
                events.preventDefault();

                var url = $("#url").val();
                var address = $("#address").val();
                var dataString = 'url=' + url + '&address=' + address;

                $.ajax({
                    type: "POST",
                    url: "read.php",
                    data: dataString,
                    success: function() {
                        var result = "<?php echo getme($url,$address);  ?>";
                        alert(result);
                        $('#times').append('<li></li>');
                    }
                });
                return true;
            });
        </script>

        <?php 

            function getme($url, $address) {
                $html = file_get_html($url);
                $code = $html->find($address, 0);
                return $code;
            }

            echo getme($url, $address);

        ?>
    </body>
</html>

无标题1
$(“#发送”)。单击(函数(事件){ events.preventDefault(); var url=$(“#url”).val(); var address=$(“#address”).val(); var dataString='url='+url+'&address='+address; $.ajax({ 类型:“POST”, url:“read.php”, 数据:dataString, 成功:函数(){ var结果=”; 警报(结果); $(“#次”)。追加(“
  • ”); } }); 返回true; });
    任何人都能帮助我

    Thanx all

    试试这个:

    <!DOCTYPE html>
    <html>
        <head>
            <title></title>
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
            <script type="text/javascript">
                $(document).ready(function(){
    
                    function timer(){
                    $.post( "process.php", function( data ) {
                        $('#times').append('<li>'+data+'</li>');
                    });
                    }
    
                    setInterval(function(){timer()},1000);
    
    
                });
            </script>
        </head>
    
        <body>
             <ul id="times"></ul>
        </body>
    </html>
    
    这是我的测试结果:

    • 上午11:15:43
    • 上午11:15:46
    • 上午11:15:51
    • 上午11:15:53
    • 上午11:15:53
    • 上午11:16:10
    • 上午11:15:52
    • 上午11:15:42
    • 上午11:16:09
    • 上午11:16:17
    • 上午11:16:12
    注意:

    1-建议将间隔从1000毫秒(1秒)改为1000毫秒

    2-在特定重复之后,不要忘记使用
    clearInterval()

    <?php
        include 'simple_html_dom.php';
        $html = file_get_html('http://www.time.is/');
    
        foreach($html->find('#clock0') as $element){
            echo $element->plaintext;
        }
    ?>