【问题标题】:Delete record without a page refresh with ajax in php在php中使用ajax删除记录而不刷新页面
【发布时间】:2013-08-03 13:19:23
【问题描述】:

我正在开发 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>

该代码运行良好,只是在删除图像或记录后,记录/图像仍会显示在页面上,直到刷新为止。我该如何解决这个问题?

【问题讨论】:

  • $('.image_or_file_selecor).hide();试试这个 ajax 成功
  • @IgnatB。图片在 foreach 循环中......我的意思是页面上有多个图片..如果我按照你的方式这样做,那么它也会隐藏其他图片..你不觉得吗?
  • 你应该考虑使用cakebook.cakephp.org/2.0/en/core-libraries/helpers/…的JsHelper中的链接功能
  • for 循环是什么样的?实际图像如何显示?
  • @putvande 我更新了我的问题

标签: php javascript jquery ajax cakephp


【解决方案1】:

您需要使用 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; 没有任何意义,因为您将文件名参数分配给具有相同名称的新变量。您可以将其删除。

【讨论】:

  • 那么路径是私有的 .. 不是任何人都可以访问图像 .. 图像与特定用户相关联...所以我如何在这里给出路径
  • $('#' + idImage').remove();我想使用这种方法,但我应该在这里写什么而不是 hash'#'
  • 谢谢...我已经用你的方法做到了...谢谢它有效
  • 没问题。我是来帮忙的。 :)
猜你喜欢
  • 1970-01-01
  • 2021-05-28
  • 1970-01-01
  • 2017-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-09
  • 1970-01-01
相关资源
最近更新 更多