成功功能不起作用

时间:2016-03-28 08:18:02

标签: php jquery

我试图用ajax删除图片框,当我点击删除但我的图片已从数据库中删除并从文件夹中取消链接时,我的class="box"没有消失,任何人都可以看到是否有人我的代码有问题,提前谢谢!

我的代码:

<ul>
<?php while ($result = mysql_fetch_array($sql)) { ?>
    <li class="box">
        <div class="image-box">
            <div class="items-image" style="background-image: url(upload/<?php echo $result['img_1']; ?>);"></div>
            <p class="error" style="display: none;">Can't delete</p>
            <div class="items-footer">
                <a class="delete" id="<?php echo $result['img_id']; ?>"><i class="fa fa-trash fa-lg"></i></a>
            </div>
        </div>
    </li>
<?php } ?>
</ul>

$(document).ready(function(){
    $(".delete").on('click', function(evt){
    var del_id = $(this).attr('id');
    var prop_id = "<?php echo $ID; ?>";
        $.ajax({
            type:'POST',
            url:'image_delete.php',
            data: ({delete_id : del_id, product_id: prop_id}),
            success: function(data) {
                if(data == '0') {
                    $(".error").show();
                } else if(data == '1') {
                    $(this).parents(".box").animate({ backgroundColor: "#003" }, "slow").animate({ opacity: "hide" }, "slow");
                }
            }
        }); 
    });
});

image_upload.php

<?php 
include 'connect.php';
$path = "upload/";
if(isset($_POST['delete_id'])){
    $delete_id = $_POST['delete_id'];
    $product_id = $_POST['product_id'];

    $check = mysql_query("SELECT COUNT(img_id) as cnt FROM fm_product_image WHERE p_id_img = '$product_id'") or die(mysql_error());
    $getcheck = mysql_fetch_array($check);

    if($getcheck['cnt'] == '1') {
        echo '0';
    } else {
        $sql = mysql_query("SELECT img_1 FROM fm_product_image WHERE img_id = '$delete_id'") or die(mysql_error());
        $result = mysql_fetch_array($sql);
        $delete = $result['img_1'];
        $unlink = $path.$delete;

        if (unlink($unlink)) {
            mysql_query("DELETE fm_product_image FROM fm_product_image WHERE img_id = '$delete_id'") or die(mysql_error());
            echo '1';
        } else {
            echo '0';
        }
    }
}
?>

1 个答案:

答案 0 :(得分:1)

使用删除:

$(document).ready(function(){
    $(".delete").on('click', function(evt){
    var del_id = $(this).attr('id');
    var prop_id = "<?php echo $ID; ?>";
    var box = $(this).closest(".box");
        $.ajax({
            type:'POST',
            url:'image_delete.php',
            data: ({delete_id : del_id, product_id: prop_id}),
            success: function(data) {
                if(data == '0') {
                    $(".error").show();
                } else if(data == '1') {
                    box.animate({ backgroundColor: "#003" }, "slow").animate({ opacity: 0 }, "slow");
                    box.remove();
                }
            }
        }); 
    });
});

注意:$(this)表示ajax对象,不透明度是1到0之间的值