【问题标题】:Updating, Deleting, Editing data更新、删除、编辑数据
【发布时间】:2018-07-29 06:01:39
【问题描述】:

我的英语不好。对不起。我相信你明白我的意思。 我在下面分享的代码质疑是否有新内容,如果有,请将新内容添加到页面中。但我不知道更新或删除相同或不同(id)需要什么逻辑。如何进行更新和删除?

index.php

<?php require "ayar.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title>jQuery Ajax ile Anlık Veri Güncelleme Uygulaması</title>
    <style type="text/css">
    ul, li {padding: 0; margin: 0; list-style: none; font: 14px Arial}
    ul li {padding: 5px; background: #eee; margin-bottom: 5px}
    ul li.new {background: lightgreen}
    </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="ajax.js"></script>
</head>
<body>

<ul>
    <?php
    $query = mysql_query("SELECT * FROM veri ORDER BY veri_id DESC");
    while ($row = mysql_fetch_object($query)){
        echo '<li id="'.$row->veri_id.'">'.$row->veri_text.'</li>';
    }
    ?>
</ul>

<div id="sonuc"></div>

</body>
</html>

ajax.php

<?php

    require "ayar.php";

    if ($_POST){

        $lastid = $_POST["lastid"];
        if (!$lastid){
            $array["hata"] = "Geçersiz işlem!";
        } else {

            $query = mysql_query("SELECT * FROM veri WHERE veri_id > $lastid ORDER BY veri_id DESC");
            if (mysql_affected_rows()){
                while ($row = mysql_fetch_object($query)){
                    $array["veriler"] = '<li class="new" id="'.$row->veri_id.'">'.$row->veri_text.'</li>';
                }
            }

        }

        echo json_encode($array);

    }

?>

ajax.js

$(function () {

    $ajaxLoad = function () {

        var lastid = $("ul li:first").attr("id");

        $.ajax({
            type: "post",
            url: "ajax.php",
            data: {"lastid": lastid},
            dataType: "json",
            success: function (result) {
                if (result.error) {
                    $("#result").html(result);

                }
                else{
                    $("ul").prepend(result.data);
                }
            }

        });
    }
    setInterval("ajaxLoad()",5000);

【问题讨论】:

标签: php jquery css ajax


【解决方案1】:

在您的 ajax.js 的最后一行中,您缺少函数名称上的美元符号 $,您应该有 setInterval("$ajaxLoad()",5000);

【讨论】:

    【解决方案2】:

    在带有 jQ​​uery 的 Javascript 中,您可以:

    • 使用$( "selector" ).length 检查元素是否存在
    • 使用$( "selector" ).remove() 删除项目。
    • 使用$( "selector" ).html() 更新内容。

    其中 selector 是一个 rowid(例如:#123

    因此,在您的成功处理程序中,您可以将 $("ul").prepend(result.data); 替换为:

    1. 获取现有&lt;li&gt; id 列表的东西(例如:idList = jQuery.map(jQuery("ul li"),function(e){return e.id})
    2. 检查新结果是否存在(例如:idList.indexOf(results.data.test(/id="(.*?)"/) &gt; 0

    ...然后决定是否插入/更新/删除到列表中。

    【讨论】:

    • 我不知道在哪里添加代码以及如何运行它。我刚开始。上面的代码不是我自己写的。你能帮助我吗?应该不难吧?
    【解决方案3】:

    根据您的代码,您每 5 秒通过 ajax 查询更新或加载数据。 $ajaxLoad() 是一个函数而不是字符串。所以你的 setInterval 函数应该是这个

            setInterval($ajaxLoad(), 5000)
    

    对于删除相同或不同的id:
    你想什么时候做呢?在像click 这样的任何特定操作之后,如果是,那么您也可以通过 ajax 查询来做到这一点。

    【讨论】:

      猜你喜欢
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多