繁体   English   中英

AJAX要求刷新而不获取数据值

[英]AJAX call for refreshing not getting data- value

我正在为我的网站创建一个聊天框,但是当我尝试获取数据值时,结果是不确定的。 我的JavaScript如此调用AJAX到我的fetchChat.php ...

var myVar = setInterval(function () {refreshChat()}, 5000);

function refreshChat() {
    var lastItem = 0;
    lastItem = $(".chatmessage:last-child").data('value');
    if (typeof lastItem === 'undefined')
    {
        alert("undefined");
        lastItem = 0;
    }

    $.post("outputPages/fetchChat.php", {'li':lastItem}, function(response)
    {   
        $(".messagefeed").append(response);
    });
}

我在fetchChat中的PHP是:

<?PHP
require "../pages/connect.php";

$li = $_POST;
$li = implode($li);

$fetch = "SELECT * FROM chat WHERE messageID >= '$li' ORDER BY messageID ";

if ($result = $mysqli->query($fetch)) {
    while ($row = $result->fetch_array()) {
        $ID = $row['messageID'];
        $date = $row['messageDate'];
        $persona = $row['messagePersona'];
        $message = htmlspecialchars($row['message'], ENT_QUOTES);
        echo "<p class='chatmessage' data-value='$ID'>$date $persona:     $message</p>";
    }
}
?>    

因此,基本上,我使用最后一条消息的数据值来使所有消息大于最后一条消息。 但是,每当我在javascript中回显lastItem时,它都会返回undefined。 任何帮助都非常感谢! 这是我自己的想法,而我正在尝试看看到目前为止我能用我的学校知识做什么。

固定! 我必须将我的lastItem更改为$(“。chatmessage:last-child”)。attr('data-value');

谢谢Roumelis的帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM