簡體   English   中英

刪除記錄而不用PHP中的Ajax刷新頁面

[英]Delete record without a page refresh with ajax in php

我正在開發Cakephp 2.x,但我認為問題與Cakephp沒有任何關系。 我想刪除文件而不刷新頁面。

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");
        }
    });
}

我的圖像在foreach循環中,此代碼顯示圖像

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)
        );?>

刪除連結

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

該代碼非常有用,除了刪除圖像或記錄后,記錄/圖像仍顯示在頁面上,直到刷新為止。 我該如何解決?

您需要使用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);
            }
        }
        ...
    });

注意: var filename = filename; 沒有任何意義,因為您正在將文件名參數分配給具有相同名稱的新變量。 您可以刪除它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM