I want to add new row to div.list once a second without deleting or refreshing the last row or refreshing the page. How would I go about achieving this?
Here is my code so far (just replace content of test2.php to xx to make it work):
<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>
This is the code in my page:
<div class="list">
<div>new row1</div>
<div>new row2</div>
<div>new row3</div>
<div>new row4</div>
.
.
.
</div>
You can append to your list what you want without delete last row
<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>';
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.