如何在没有任何交互的情况下从数据库检索数据


how do i retrieve data from database without any interaction?

简单的问题,我如何从数据库检索数据没有任何交互?

示例:有人给我发了一条消息,我想在不更新页面的情况下阅读它。

我已经成功地将数据插入数据库而没有更新页面,但现在我必须检索它。

你可以在后台做一个简单的脚本调用getnewinfoo .php和发送一个简单的ajax调用,最后的消息id你打印在你的页面上(即:隐藏在输入文本),所以脚本将返回任何新的消息与日期或id大于一个你正在发送。然后简单地更新html dom上的任何元素,比如用简单的jquery追加一个div。HTML:

<div id="message">My current message</div>
<input type="hidden" name="" value="242" id="lastid">

Javascript:

$.ajax({
        url: "getnewinfo.php",
        data: {
            lastid: $('#lastid').val() //send to your php the last id you printed so it search for any new id different than that one
        },
        type: "GET",
        dataType: "html",
        success: function (data) {
            $('#message').append(data); //Append the new received data                
            //Or replace the current value with the new one
            $('#message').html(data); //Replace with the new received data   
        },
        error: function (xhr, status) {
            alert("Sorry, there was a problem!");
        },
        complete: function (xhr, status) {
        }
    });

您可以使用HTML和刷新标记来刷新整个页面,或者您可以使用JS每隔一段时间检查新消息,并且使用一些ajax,仅刷新显示消息的页面部分