簡體   English   中英

每秒通過php mysql jquery添加新行

[英]add new row every second via php mysql jquery

我想每秒將新行添加到div.list中,而不刪除或刷新最后一行或刷新頁面。 我將如何實現這一目標?

到目前為止,這是我的代碼(只需將test2.php的內容替換為xx即可使用):

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        jQuery(function($){
  setInterval(function(){
    $.get( 'http://example.com/php/test2.php', function(newRowCount){
      $('#rowcounter').html( newRowCount );
    });
  },1000); //
});
    </script>
</head>
<body>
    <div id="log">

    </div>
    <p>There are <span id='rowcounter'>xx</span> rows in the DB.</p>

</body>
</html>

這是我頁面中的代碼:

<div class="list">
   <div>new row1</div>
   <div>new row2</div>
   <div>new row3</div>
   <div>new row4</div>
.
.
.
</div>

您可以將所需內容添加到列表中,而無需刪除最后一行

    <html>
    <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        jQuery(function($){
  setInterval(function(){
    $.get( 'http://example.com/php/test2.php', function(newRowCount){
      $('#rowcounter').append( newRowCount );
    });
  },1000); //
});
    </script>
</head>
<body>
    <div id="log">

    </div>
    <p>There are <span id='rowcounter'>xx</span> rows in the DB.</p>

</body>
</html>
<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
    <script type="text/javascript">
    jQuery(function($){
        setInterval(function(){
            $.ajax({
                url: "http://example.com/php/test2.php",
                type: "GET",
                data: {id : $('#list div').length},
                dataType: "html",
                success: function( data ) {
                    $('#list').append( data );
                    $('#rowcounter').html( $('#list div').length );
                }
            });
        },1000); 
    });
    </script>
</head>
<body>
    <div id="list">

    </div>
    <p>There are <span id="rowcounter">0</span> rows in the DB.</p>

</body>
</html>





<?php 

//Listing: http://example.com/php/test2.php

$id = isset($_GET['id'])?$_GET['id']:0;

echo '<div>new row'.$id.'</div>';

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM