0
<?php     
 $imgcheck=mysql_query("

  SELECT *
FROM `images`
 WHERE `deal_id` =$_SESSION[eid]
LIMIT 0 , 30

 ");
 $numimgcheck=mysql_num_rows($imgcheck);
  if($numimgcheck==0){echo '<span style=color:#ff0000; >No pictures uploaded</span>';}
    while ($rowimg2= mysql_fetch_array($imgcheck)){ 


     $imgname=$rowimg2['name'];

     {
    //   $rowimg2 = mysql_fetch_array($imgcheck) or die(mysql_error());

     //echo $imgname;
     echo '    <a href="users/'.$_SESSION['userid'].'/images/'.$imgname.'"   rel="lightbox[slide]" caption=".">';
       }


       { echo '<img src="users/'.$_SESSION['userid'].'/images/thumbs/'.$imgname.'"   border="0" />';}
     { echo '</a><a href="delimg.php?id='.$imgname.'"><img src="images/del.jpg" width="22" height="22" onclick="deleteImg(this);" />   </a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     ';

    }

  //<input type="checkbox" name="'.$imgname.'" id="'.$imgname.'" > if($numimgcheck==0){      $havepic=' <td width="50px" align="center"><div class="iddivimg"> <img src="users/default/nopic_small.jpg"  />

// ';}

    }
    ?>

There is a page in my website where users can edit and delete images. In this page, there is a delete button (itself a 20x20 image) near every image. When the user clicks on that button, its corresponding image will be deleted. I want it to be in such a way that when I click the delete image a confirm box should appear, asking "Are you sure you want to delete this image?" If I click yes, I want to delete the MySQL table's data and image. If I click no, nothing should happen. How can I do this?

15
  • Please tag with server-side language (assuming PHP?) Commented Jun 16, 2011 at 3:16
  • @mellamokb previously it was tagged as Java ... so I assumed .. Commented Jun 16, 2011 at 3:20
  • This is more of a Javascript question and not a MySQL, so I think you should remove the "mysql" tag Commented Jun 16, 2011 at 3:20
  • @Abhay Gupta: This is very much a MySQL question, and some server-side language must be involved (i.e., php). How else are you going to "delete the MySQL table's data and image" (straight from OP)? Commented Jun 16, 2011 at 3:22
  • 1
    @Chamara Denipitiya: Please comment on the appropriate answer that you tried with what happened, and what you expected to happen. Can you edit and add a sample of the code you are trying to your question? Commented Jun 16, 2011 at 3:33

6 Answers 6

2

Put each image in its own form with the image identifier as hidden value and use the delete image as CSS background image of the submit button. Finally use JS confirm() function for the confirmation.

<form action="delete.php" method="post">
    <img src="image1.png" />
    <input type="hidden" name="id" value="1" />
    <input type="submit" class="delete" value="" onclick="return confirm('Sure?')" />
</form>
<form action="delete.php" method="post">
    <img src="image2.png" />
    <input type="hidden" name="id" value="2" />
    <input type="submit" class="delete" value="" onclick="return confirm('Sure?')" />
</form>
...

with CSS

.delete {
    background-image: url('delete.png');
    width: 20px;
    height: 20px;
    border: 0;
    cursor: pointer;
}

This is easily done in a loop. You could use jQuery to unobtrusively introduce ajaxical powers, whenever necessary.

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

7 Comments

lol @ "unobtrisuvely introduce ajaxical powers, whenever necessary"
@BalausC i'd suggest moving the confirm('sure?') into its own function like: function sure(){ return confirm('Are you sure?');} only so that if you want to alter the wording it is very easy. Otherwise top answer! +1
@James: if you print this in a loop, you'd need to specify it only once in the PHP template, right? :)
@BalusC Sure ... There are other reasons to put it in another function but without assuming it is being rendered by a script. (some other reasons: reduced markup? adding other logic? ability to change what the onclick event does via javascript? using it in other places also? multilingual site?) ... I guess it is personal preference I just always like to think of the future.
@James: True. Depends on too many things. Server side logic is not so relevant in this particular question. The code snippet was just a kickoff example to get the idea how the final HTML should look like :)
|
1

Its hard to help without some sample code. assuming you have a function deleteImg() and an image <img src="delete.jpg" onclick="deleteImg(this);" />

then you do:

function deleteImg(image)
{
  if(confirm("are you sure you want to delete this image"))
  {
      ... // delete image logic.
  }
}

http://www.javascripter.net/faq/confirm.htm

5 Comments

i tried this script and ,confirmation box appears, but it don't stop deleting image.which means if user clicked on yes it continues and if user clicked cancel it also countinue deleting
@chamara As i said i've assumed a lot as there is little code explaining what you have. but if you put all the logic that does the deleting inside the if and not outside... it should work. try if(confirm("sure")){alert("yes");} you should only see the alert if you click yes.
@chamara firstly Its a standard to never do any major action (like deleting) on a GET I suggest you follow one of the other examples that does it on a POST. I suggest BalusC's answer. Secondly this is assuming you are doing it onclick and you are using an achor tag (<a>).
@Chamara you did not put the deleteImage function. I guess problem lies in that one.
@Chamara i'd also suggest having a quick read through cs.tut.fi/~jkorpela/forms/methods.html
1

What server-side language is being used? Assuming PHP for the purposes of this explanation.

Rough sketch (AJAX method):

  1. Create a server-side page that can be called to delete an image (say, delete_image.php?image_id={id})
  2. Onclick on the image, use confirm to validate user wants to continue.
  3. If confirm was true, create an AJAX post to the delete_image call (see jQuery library for easy method to do this).
  4. Remove image from the page (or refresh).

Form POST method:

  1. Wrap every delete image button in a form, which posts to delete_image.php, with a hidden input variable containing the image id.
  2. Onclick (or onsubmit) use javascript confirm, and only continue on true result.
  3. In delete_image.php, retrieve image_id from post form
  4. Delete corresponding image from MySQL database with a DELETE query.
  5. Redirect back to image display page to refresh with image removed.

Comments

1

It seems you haven't tried searching the net for solution because I'm sure you will find some if not many. When clicked on the image, call a javascript function that prompts the user with a question, using the JS confirm() function, like

if (confirm("Do you want to delete the image?")) {
    // call your PHP script that will then delete the image
}

8 Comments

@Trufa: OP's question history confirms that he's familiar with that.
@Trufa: I assumed PHP because the question referred to MySQL, of course it can be any server-side language. And the question is as well tagged to PHP now, so I guess my assumption was correct
@Abhay Gupta: Just an FYI: I tagged it PHP... it may be wrong :-)
@AbhayGupta: Indeed, and in fact it speaks bad about the answer in any case, not so much your answer.
@AbhayGupta: Ohh and sorry if it sounded aggressive, it was never the intention.
|
1

In the code that you had recently added to your question, I think the issue is that the <a href> is always executed because it runs independent of your call to the function deleteImg().

<a href="delimg.php?id='.$imgname.'">
    <img src="images/del.jpg" width="22" height="22" onclick="deleteImg(this);" />
</a>

I think if you change your code to this, it should work:

<a href="delimg.php?id='.$imgname.'" onclick="deleteImg(this);">
    <img src="images/del.jpg" width="22" height="22"  />
</a>

And in your deleteImg() function, add return TRUE; in the IF part of your confirm() and add return FALSE in the ELSE part. Something like this:

if (confirm("Do you want to delete the image?")) {
    // call your PHP script that will then delete the image
    return TRUE;
} else {
    return FALSE;
}

1 Comment

Another alternative is that instead of using the anchor tag, you can call the delimg.php script directly inside the confirm IF() using "document.href" (I might not fully remember the syntax). Or perhaps use an AJAX call, but I think that's more of research and learning that you should try to do.
0

Well there is no single solution to this so I'll provide some technologies that will be useful for you (I assume from the tag that you are using Java environment on server).

As for the confirm box, In case you mean something like DHTML confirm boxes that are commonly seen on Facebook, you might want to use jQuery UI component. Have a look at this : http://jqueryui.com/demos/dialog/#modal-confirmation

You can also use javascript confirm function, but these boxes are currently in vogue.

As far as updating the database is considered, of course that has to be done from server side. You can set a Servlet to do the deletion or a web service for the same. It just is a matter of passing right parameters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.