Php 仅在提交表单后删除图像

Php 仅在提交表单后删除图像,php,jquery,Php,Jquery,上传图片后,我想让用户可以通过点击图片上的“x”来删除它。我尝试了一些在线搜索的jquery代码,但没有得到我想要的结果。是否只有在单击submit按钮时才可以从数据库中删除图像,而不是重定向到删除页面,然后返回删除另一个图像 if($res18 && mysql_num_rows($res18) > 0){ while($row18 = mysql_fetch_assoc($res18)){ if (file_exists($row18['ima

上传图片后,我想让用户可以通过点击图片上的“x”来删除它。我尝试了一些在线搜索的jquery代码,但没有得到我想要的结果。是否只有在单击submit按钮时才可以从数据库中删除图像,而不是重定向到删除页面,然后返回删除另一个图像

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">';       
        }
    }
} 
我假设逻辑是这样的:单击“x”链接后,运行一个运行delete查询的函数

感谢您的帮助

更新:

$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>

list.php可以是这样的:

<?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();
            });
        });
    });

"
id=“img——”
class=“图片”>
}
}
}
?>
//加载页面时执行此操作
$(文档).ready(函数(){
//为所有具有“addition\u image\u close”类的“a”元素设置onclick
$('a.addition\u image\u close')。单击(函数(){
var$id=$(this.prop('data-id');
//发布到delete.php,发送id=$id作为数据并设置成功处理程序
$.post('delete.php',{id:$id},function(){
//从页面中删除元素
$('#img-'+$id).remove();
$('#a-'+$id).remove();
});
});
});

使用
delete.php
接收
id
参数并执行删除查询

时,请澄清您的过程。图像是否通过jQuery上传?然后您需要一个单独的控制器来处理该请求,并在成功发布后使用JS执行一些dom操作。如果您发布一整页文章,则可以生成DELte链接完全不使用JS。否正在使用php插入查询上载图像(更新了上面的代码)。这就是为什么我要问的原因,因为我对JQuery没有太多的经验。也许你应该多读一些关于sql、php、html和js是如何相互关联的。你可以从html页面开始上传,该页面可能是由php脚本生成的。这里只有html和js。因此,你最有可能使用常规html表单将图像发布到uploaded,然后用php脚本处理。上传图像后,你呈现列表页面。在那里你只能呈现html和js代码。js代码可以是一个jQuery操作,让X按钮请求php后端脚本删除图像,然后通过DOM操作将图像从页面中移除。我明白你的意思。对吗现在,图像都是使用php查询加载到html页面上的。我还没有使用任何js代码。我猜,你所说的制作x按钮的jquery操作就是我所想的。有没有教程说明如何做到这一点?或者你能给我举个例子吗?基本上使用与插入相同的格式,但使用DELE就像你现在一样,在函数和条件语句中设置了一个与上传文件变量相关的WHERE子句。请看我上面发布的更新代码。使用你的代码似乎仍然不起作用。即使在修复了一些小的语法错误之后。我觉得它非常接近,但还是有什么想法遗漏了为什么不起作用?我一直在想办法,但我看不出有错
$id = $_GET['id'];
mysql_query("DELETE FROM `images` WHERE `ad_id` = '".$id."'");
<?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();
            });
        });
    });