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

标签 php jquery mysql sql ajax

<分区>

简单的问题,如何在没有任何交互的情况下从数据库中检索数据?

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

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

最佳答案

好吧,你可以在后端做一个简单的脚本,叫做 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/

相关文章:

PHP7 - 连接到 sybase 数据库

php - 获取 woocommerce_order_status_completed Hook 上的用户 ID

javascript - 在 .js 中设置默认选项卡

jquery - 让按钮将背景颜色切换为显示元素的背景颜色

c# - 检查mysql数据库中是否存在

php - 我的 iPhone 应用程序出现 "Certificate Invalid"错误

php - Laravel NotFoundHttpException 虽然路由存在

PHP 启动 : Unable to load dynamic library for PHP folder

jquery - 在 HTML 选择元素中使用 attr 显示图像

MySQL 查询未使用多个 AND 运算符进行编译