0

如何使动画表仅在将新记录添加到数据库时才动画。

Ajax/Js(在 index.php 中):

$.ajax({
url : 'Not sure what goes here?',
data : {Not sure what goes here?},
dataType : 'application/json', // Is this correct?
success: function(response) {
    // Only when successful animate the content
    newItem(response);
} 
});

var newitem = function(response){
var item = $('<div>')
    .addClass('item')
    .css('display','none')
    .text(response)
    .prependTo('#scroller')
    .slideDown();
$('#scroller .item:last').animate({height:'0px'},function(){
    $(this).remove();
});
}

我的 php (latest.php):

include ('db.php');
$sql2 = "SELECT * FROM `feed` ORDER BY `timez` DESC";
$res2 = mysql_query($sql2) or die(mysql_error());
while($row3 = mysql_fetch_assoc($res2)){

$user = $row3['username1'];
$action = $row3['action'];
$user2 = $row3['username2'];

echo ''.$user.''.$action.''.$user2.'<br>'; // Needs to be a json array?

我无法让它工作,这是表的操作方式http://jsfiddle.net/8ND53/谢谢。

4

3 回答 3

1
$.ajax({
 url : your_php_file.php',
 data : {data you'r going to send to server}, // example: data: {input:yourdata}
 success: function(response) {
     $('#table_id').append('<tr>'+response+'</tr>'); // response is the date you just inserted into db    
 } 
});

your_php_file.php

add the item into db
echo that inserted data # if you echo, then you can catch this with ajax success function then you append it into your table. 
于 2013-06-03T10:18:20.133 回答
1

尝试填写如下:

$.ajax({
type: "post"
url : 'locationOfphpCode/phpCode.php',
data : {data you want to pass}, //{name: "dan"}
success: function(response) {
    // Only when successful animate the content
    newItem(response);
} 
});

在您的 php 代码中,您需要接收从 ajax 调用传递的数据:

<?php
$name = $_POST['name'];
...
?>

您可以在您的 php 代码中添加一些验证。

希望这会帮助你。

于 2013-06-03T10:22:08.773 回答
0

你给出的例子是使用setInterval(newitem, 2000)

所以你必须在某个固定的时间间隔内调用 ajax 函数。

于 2013-06-03T10:16:54.917 回答