Passing specific value via POST from <img> to PHP


Passing specific value via POST from <img> to PHP

我得到了一个用户应该能够点击的<img>(例如删除墙上的帖子)。每个图像都得到与文章本身的ID相匹配的"imageid"。现在我想通过javascript/jQuery/任何传递这个"imageid"到我的delete.php文件,在那里我将该ID发送到我的Oracle数据库,以知道要删除的文章。

IMGs看起来像:

 <img alt="Delete Article" src="include/images/delete.png" imageID="21" title="Delete Article">

和javascript:

$(document).ready(function erase() {
    $("img[title='Delete Article']").click(function() {
        if (confirm("Are you sure?")) {
            var image = $(this).data('imageID');    
            $.ajax({
                 type: "POST",
                 url: 'include/site/delete.php',
                 data: 'imageID=' + image,
                });
        }
    });
});

我只找到了创建隐藏表单标签并以这种方式发送它的方法,但我想以另一种方式(如果可能的话)做到这一点:)

Thanks in advance

找到解决方案!

$(document).ready(function () {
        $("img[title='Delete Article']").click(function() {
            if (confirm("Are you sure?")) {
                var getimageID = $(this).attr('imageID');
                $.post("include/site/delete.php", {
                    catchedID : "ID ist " + getimageID
                });
            }
        });
    });

你应该在你的图像标签上使用一个数据属性,比如'data- imageid ',然后

<img alt="Delete Article" src="include/images/delete.png" data-imageid="21" title="Delete Article">
//jquery in your if(confirm)
var $post = ;//some selector for the post/image
var testvalue = $(this).data('imageId');
$.ajax({
 type: "POST",
 url: 'delete.php,
 data: 'imageId=' + testValue,
 success: function(data){},
  $post.remove();
});

info here http://api.jquery.com/jQuery.post/

var testvalue = $(this).attr('imageid');

如果您愿意,可以使用GET或POST发送它。对于GET,只需像这样构建链接:

    <a href="delete.php?imageid=<?php echo($imageid);?>"
 onClick="return confirm('Are you sure?');">Click to Delete</a>

您还可以使用POST,将每个条目转换为自己的表单,然后将图像id添加为隐藏值。

请记住,这两种方法都不是很安全,您需要确保delete.php不会在没有适当权限的情况下删除内容。