0

I got an <img> which user should be able to click (to delete a post on a wall for example). Every image got the "imageid" which matches the ID of the article itself. Now I want to pass this "imageid" via javascript/jQuery/whatever to my delete.php file where I send that ID to my Oracle database to know what article to delete.

IMGs looks like:

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

And the 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,
                });
        }
    });
});

I only found the way to create a hidden form tag and to send it that way, but I want to do it (if possible) in another way :)

Thanks in advance

Found solution!

$(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
                });
            }
        });
    });
5
  • possible duplicate of How to pass variable from JavaScript to PHP using jQuery POST Commented Apr 22, 2013 at 11:50
  • (Note that this specific question is one step ahead of you in terms of how to do things) Commented Apr 22, 2013 at 11:50
  • It would be better to use the valid data-* attribute instead. data-imageid should replace imageid. Commented Apr 22, 2013 at 11:51
  • alert($(this).attr("imageid")) shows me the ID as well but still got the problem of passing it to delete.php. Tried to echo it there but with no result :/ Commented Apr 22, 2013 at 11:59
  • no duplicate since ur posted link uses "submit" (think hes also using formula?) which I dont want to use Commented Apr 22, 2013 at 12:03

3 Answers 3

1

you should use a data attribute such as 'data-imageId' on your image tag and then do

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

Sign up to request clarification or add additional context in comments.

10 Comments

Looks great! How can I now call the value in my delete.php? with $_POST['data'] ? And sorry I'm a beginner but what to fill in the var $post in my example code?
Just use $_POST['imageId']
var $post would be the selector to get the actual post in your html. I don't know what your html looks like for posts so I can't advise!If the image is an immediate child of it then something like var $post = $(this).parent(); might work
SyntaxError: unterminated string literal url: 'delete.php,
Undefined index: imageID :(
|
0
var testvalue = $(this).attr('imageid');

Comments

0

You can send it using GET or POST if you'd like. For GET, just build the link as such:

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

You can also use POST by turning each item into its own form, then adding the image id as a hidden value.

Keep in mind that neither one of these is very secure, and you're going to want to make sure that delete.php doesn't just delete things without the person actually having proper permissions to delete it.

1 Comment

yeah ill add permission later on :D I dont want to have some text just the picture and better POST than GET but thanks so far

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.