Pergunta

Este script recebe tempo em www.time.is e do #twd ID e mostre ao clicar no envio BTN.

Eu tenho dois problemas:

1 somente show primeiro tempo de recebimento e não atualize quando clique em Enviar novamente

2- Página Atualizar e perder dados

Eu tenho três problemas com o código abaixo:

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

Qualquer corpo pode me ajudar ..

Obrigado tudo

Foi útil?

Solução

Experimente isso:

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

E coloque isso em seu process.php Arquivo:

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

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

Este é o resultado do meu teste:

  • 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

Observação:

1- É aconselhável que você altere intervalos de 1000 milissegundos (1 segundo).

2- Não se esqueça de usar clearInterval() Após repetição específica.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top