Search code examples
phpajaxexternalauto-updateajaxform

get and show constant from external webpage and update when change


this script receive time from www.time.is and from #twd ID and show when click on the send btn.

I have two problems:

1-only show first receive time and don't update when click on send again

2- page refresh and lose data

I have Three problem with below code:

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

any body can help me ..

Thanx all


Solution

  • Try this:

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

    And put this in your process.php file:

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

    This is my test result:

    • 11:15:43AM
    • 11:15:46AM
    • 11:15:51AM
    • 11:15:53AM
    • 11:15:53AM
    • 11:16:10AM
    • 11:15:52AM
    • 11:15:42AM
    • 11:16:09AM
    • 11:16:17AM
    • 11:16:12AM

    Note:

    1- It is advisable that you change intervals from 1000 milliseconds (1 second).

    2- Don't forget to use clearInterval() after specific repeat.