<分区>
简单的问题,如何在没有任何交互的情况下从数据库中检索数据?
示例:有人给我发了一条消息,我想在不更新页面的情况下阅读它。
我已经成功地将数据插入数据库而没有更新页面,但现在我必须检索它。
<分区>
简单的问题,如何在没有任何交互的情况下从数据库中检索数据?
示例:有人给我发了一条消息,我想在不更新页面的情况下阅读它。
我已经成功地将数据插入数据库而没有更新页面,但现在我必须检索它。
最佳答案
好吧,你可以在后端做一个简单的脚本,叫做 getnewinfo.php 并用一个简单的 ajax 调用发送,你在页面上打印的最后一条消息 ID(即:隐藏在输入文本中)这样脚本将返回任何新的日期或 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) {
}
});
关于php - 如何在没有任何交互的情况下从数据库中检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32828725/