该脚本从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>

任何身体都可以帮助我。

thanx全部

有帮助吗?

解决方案

尝试这个:

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

并把它放在你的 process.php 文件:

<?php
    include 'simple_html_dom.php';
    $html = file_get_html('http://www.time.is/');

    foreach($html->find('#clock0') as $element){
        echo $element->plaintext;
    }
?>

这是我的测试结果:

  • 上午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秒)更改。

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top