weixin_33675507 2018-02-01 15:23 采纳率: 0%
浏览 32

通过Ajax删除图像

Very recently I have started programming and I'm currently running into an issue to which I hope you can help:

  • Ajax executes the upload.php page which succesfully deletes the file from the server and from the MySQL table
  • The Ajax result however is an error message and the image is not removed from Page.php unless reloaded.

Page.php

    <div class="row no-collapse 50% uniform">
    <?php
    while($row = mysqli_fetch_array($SubEntries_SQL)) {
        $fileURL = "upload/" . $_SESSION['adminID'] . "/" . $row['filename'];
        $fileId = $row['id'];
        $fileName = $row['filename'];
        if (!file_exists($fileURL)) {  $fileURL = $sub_entryThumbURL = "../../images/no_img.png"; }

    ?>                          

        <div class="2u" id="success">                   
            <span class="image fit 50%">
                <a href="javascript:void(0)" onclick="RemoveItem('<?=$fileId;?>','<?=$fileName;?>')">
                    <img src="<?=$fileURL;?>" border="0" class="thumb" />
                </a>
            </span>
        </div>

    <?php
    }   
    ?>
    </div

Ajax function

    function RemoveItem(itemId,ItemName) {
        $.ajax({
            'url': 'content/upload.php', 
            'type': 'POST',
            'data': {itemId: itemId, name: ItemName, request: 2}, 
            'success': function(data) {
                 if (data == 1) {
                    $(".success").fadeIn(500).delay(2000).fadeOut(500);
                }
            },
            'error': function () {
                alert("error");
            }
          });
        }
  • 写回答

2条回答 默认 最新

  • weixin_33688840 2018-02-01 15:36
    关注

    There is a confusion inside the while, here's my suggestion:

    1. Where you have replace the id="success" for a unique id and common class

    Assuming your $fileId is unique among all nodes

    <div class="2u delete_success" id="#delete_<?php echo $fileId; ?>">
    

    By doing this you'll get distinct nodes, next step would be to make your javascript to know which node to delete after completion.

    The ajax function would be something like.

    function RemoveItem(itemId,ItemName) {
        $.ajax({
            'url': 'content/upload.php', 
            'type': 'POST',
            'data': {itemId: itemId, name: ItemName, request: 2}, 
            'success': function(data) {
                 if (data == 1) {
                    $("#delete_"+itemId).fadeIn(500).delay(2000).fadeOut(500);
                }
            },
            'error': function () {
                alert("error");
            }
        });
    }
    

    Changed the line: $("#delete_"+itemId).fadeIn(500).delay(2000).fadeOut(500);

    This should do the work.

    评论

报告相同问题?