繁体   English   中英

更新,删除,编辑数据

[英]Updating, Deleting, Editing data

我的英语不好。 抱歉。 我确定你明白我的意思。 我在下面共享的代码询问是否有新内容,如果有,请将新内容添加到页面中。 但是我不知道更新或删除相同或不同的(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);

在带有jQuery的Javascript中,您可以:

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

其中选择器是rowid(例如: #123

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

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

...然后确定是否要在列表中插入/更新/删除。

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

        setInterval($ajaxLoad(), 5000)

要删除相同或不同的ID:
你什么时候想做? 在执行诸如click类的任何特定操作之后,如果可以,那么您也可以通过ajax查询来执行此操作。

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

暂无
暂无

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

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