Pregunta

Este script recibe la hora de www.time.is y de #twd ID y se muestra cuando se hace clic en el botón de envío.

Tengo dos problemas:

1: solo muestra la primera hora de recepción y no se actualiza cuando se vuelve a hacer clic en enviar

Actualización de 2 páginas y pérdida de datos.

Tengo tres problemas con el siguiente código:

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

cualquiera puede ayudarme..

Gracias a todos

¿Fue útil?

Solución

Prueba esto:

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

Y pon esto en tu process.php archivo:

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

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

Este es el resultado de mi prueba:

  • 11:15:43 a.m.
  • 11:15:46 a.m.
  • 11:15:51 a.m.
  • 11:15:53 ​​a.m.
  • 11:15:53 ​​a.m.
  • 11:16:10 a.m.
  • 11:15:52 a.m.
  • 11:15:42 a.m.
  • 11:16:09 a.m.
  • 11:16:17 a.m.
  • 11:16:12 a.m.

Nota:

1- Es recomendable que cambies los intervalos a partir de 1000 milisegundos (1 segundo).

2- No olvides usar clearInterval() después de una repetición específica.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top