using java script "confirm" with PHP


I'm having some problem using Java Script and PHP together. I would like to have a link where I can delete a listed object from a DB. But the User should be asked if he really like to delete the record with a Java Script "confirm" Popup and if he choose no (Abort) the record will not be deleted.
My problem now is the record is deleted anyway doesn't matter if the user click "ok" or "Abort".
Please see my code below:

//some PHP code here......


<script language="JavaScript">
if (confirm("Do you really like to delete the System ?")) {
      //delete the IP's from the Unit
      $query = "delete from unitip where UNIT = '$id'";

// -->


//some more PHP code......


do you have any idea how to make this work?

thanks & regards
Who is Participating?
Michel PlungjanConnect With a Mentor IT ExpertCommented:
you are mixing server side and client side.
Not possible.

WHat you want is this:
<a href="#" onClick="if (confirm("Do you really like to delete the System ?")) location='myphp?delete=yes';
return false">CLick to delete</a>

or similar
what is happening is the php is getting exicuted before the confirm is even produced.  So what you need to do is have a link that will only go to the page that deletes the record when you click "OK", like what mplungjan said, or you could have one php page that will confirm and then if you click on it will foward you to delete page, if not it will bring you back a page.

Michel PlungjanIT ExpertCommented:
Any particular reason for the "B" grade?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.