duan198727 2013-08-03 13:19
浏览 41
已采纳

使用php中的ajax删除没有页面刷新的记录

I am working on a Cakephp 2.x but I don't think the problem has anything to do with the Cakephp. I want to delete a file without a page refresh.

HTML / PHP :

<div class = "success" style="display:none;">Deleted successfully </div>
<div class = "error" style="display:none;">Error  </div>    

<a href="javascript:void(0)" class="button icon-trash" title = "delete" onclick="openConfirm('<?php echo $filename; ?>','<?php echo $idImage; ?>');"></a>

JavaScript :

function openConfirm(filename, idImage) {
    $.modal.confirm('Are you sure you want to delete the file?', function () {
        deleteFile(filename, idImage);
    }, function () {

    });
};

function deleteFile(filename, idImage) {
    var filename = filename;

    $.ajax({
        type: "POST",
        data: {
            idImage: idImage
        },
        url: "http://localhost/bugshot/deleteFile/" + filename,
        success: function (data) {
            if (data == 1) {
                $(".success").fadeIn(500).delay(2000).fadeOut(500);
            } else {
                $(".error").fadeIn(500).delay(2000).fadeOut(500);
            }
        },
        error: function () {
            alert("error");
        }
    });
}

my images which is in foreach loop this code is displaying the image

foreach($file as $files):?>

   <?php    $downloadUrl = array('controller' => 'bugshot', 'action' => 'downloadImages', $files['Image']['filename'], '?' => array('download' => true));
             $imageUrl = array('controller' => 'bugshot', 'action' => 'downloadImages', $files['Image']['filename']);

        ?>
 <?php  echo $this->Html->link(
            $this->Html->image($imageUrl),
            $downloadUrl,
            array('class' => 'frame', 'escape' => false)
        );?>

Delete link

    <a href="javascript:void(0)" class="button icon-trash" title = "deleteImage" onclick="openConfirm('<?php echo $files['Image']['filename']; ?>','<?php echo $files['Image']['idImage'];; ?>');"></a>

The code works great except that after the image or record is deleted the record/image is still displayed on the page until it is refreshed. How do I fix this?

  • 写回答

1条回答 默认 最新

  • douxu2081 2013-08-03 14:36
    关注

    You need to remove it with javascript.

    $.ajax({
            ...
            success: function (data) {
                if (data == 1) {
                    $(".success").fadeIn(500).delay(2000).fadeOut(500);
                    $('img[src="/pathToImg/' + filename + '"]').remove(); // Remove by src
                    // $('#' + idImage).remove(); // Remove by ID.
                } else {
                    $(".error").fadeIn(500).delay(2000).fadeOut(500);
                }
            }
            ...
        });
    

    Note : var filename = filename; means nothing because you are assigning filename argument to a new variable with the same name. You can just remove it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)