[英]Passing specific value via POST from <img> to PHP
我得到了<img>
,該用戶應該可以單擊(例如,刪除牆上的帖子)。 每個圖像都有一個“ imageid
”,它與文章本身的ID相匹配。 現在,我想通過javascript / jQuery / whatever將此“ imageid
”傳遞到我的delete.php
文件,在該文件中,我將該ID發送到Oracle數據庫,以了解要刪除的文章。
IMG看起來像:
<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,
});
}
});
});
我只找到了創建隱藏的表單標簽並以這種方式發送的方法,但是我想(如果可能的話)以另一種方式進行:)
提前致謝
找到解決方案!
$(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();
});
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不僅在沒有人實際擁有刪除權限的情況下刪除東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.