仅在提交表单后删除图像 [英] Deleting an image only after the form is submitted

查看:51
本文介绍了仅在提交表单后删除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上传图片后,我想让用户可以选择通过单击图片上的"x"将其删除.我尝试了一些在线搜索的jquery代码,但没有得到想要的结果.是否只有在单击提交"按钮后才将其从数据库中删除的方法,而不是重定向到删除"页面然后必须返回以删除另一张图像?

After a picture is uploaded I want to let the user have the option to delete it by clicking on the 'x' on the image. I tried some jquery codes that I searched online but nothing had the result I want. Is there a way to remove the image from the database only when the submit button is clicked rather than redirecting to a delete page and then having to go back to delete another image?

if($res18 && mysql_num_rows($res18) > 0){
    while($row18 = mysql_fetch_assoc($res18)){

        if (file_exists($row18['images']) === true){    ?>
            <a href="javascript:return(0);" class="addition_images_close" onclick="delete('<?php echo $row18['id']; ?>')""></a>
            <?php                                   
            //mysql_query("DELETE FROM `images` WHERE `images`='".$row18['images']."' AND `ad_id` = '".$id."'");            
            echo '<img src="'.$row18['id'].'" class="pictures">';       
        }
    }
} 

插入查询:

mysql_query("INSERT INTO `images` ($fields) VALUES ($data)");

我认为逻辑将是这样的:单击"x"链接后,运行一个运行删除查询的函数.

I assume the logic would be somewhat like this: Once the 'x' link is clicked, run a function that runs the delete query.

感谢您的帮助!

更新:

$res18 = mysql_query("SELECT * FROM `images` WHERE `ad_id` = '" . $id . "'");
    if($res18 && mysql_num_rows($res18) > 0){
        while($row18 = mysql_fetch_assoc($res18)){
            $picture = $row18['images'];
            $pic_id = $row18['id'];

            if (file_exists($picture) === true){ ?>
                <a href="javascript:return(0);" class="addition_images_close" data-id=<?php echo $pic_id ?> id="a-<?php echo $pic_id ?>"></a>                                       
                 <img src="<?php echo $picture ?>"  id="img-<?php echo $picture ?>" class="pictures">
            <?php                   
            }
        }
    } 

<script type="text/javascript">
    $(document).ready(function(){
        // set onclick for all 'a' elements having the 'addition_image_close' class
        $('a.addition_images_close').click(function() {
            var $pic_id = $(this).prop('data-id');
            var $picture = $(this).prop('data-id');
            // post to delete.php, sending id=$picture as data and setting success handler
            $.post('delete.php', {id: $picture}, function() {
                // remove elements from page
                $('#img-' + $picture).remove();
                $('#a-' + $pic_id).remove();
            });
        });
    });
</script>

和delete.php具有以下内容:

and the delete.php has the following:

$id = $_GET['id'];
mysql_query("DELETE FROM `images` WHERE `ad_id` = '".$id."'");

推荐答案

list.php可能是这样的:

a list.php could be something like this:

<?php

// ...

// render list
if($res18 && mysql_num_rows($res18) > 0) {
    while($row18 = mysql_fetch_assoc($res18)) {
        if (file_exists($row18['images']) === true){    ?>
            <a href="javascript:return(0);"
                class="addition_images_close"
                data-id=<?php echo $row18['id'] ?>
                id="a-<?php echo $row18['id'] ?>"
            >X</a>
            <img src="<?php echo $row18['id'] ?>"
                id="img-<?php echo $row18['id'] ?>"
                class="pictures">
        }
    }
}
?>
<!-- add actions on the links rendered above -->
<script type="text/javascript">
    // do this when page is loaded
    $(document).ready(function(){
        // set onclick for all 'a' elements ahving the 'addition_image_close' class
        $('a.addition_image_close').click(function() {
            var $id = $(this).prop('data-id');
            // post to delete.php, sending id=$id as data and setting success handler
            $.post('delete.php', {id: $id}, function() {
                // remove elements from page
                $('#img-' + $id).remove();
                $('#a-' + $id).remove();
            });
        });
    });

,其中delete.php接收id参数并执行删除查询

with a delete.php receiving an id parameter and performing the delete query

这篇关于仅在提交表单后删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆