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.

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题