onClick Confirm and Execute PHP Code

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
jkwok
Forum Newbie
Posts: 13
Joined: Thu May 14, 2009 11:54 pm

onClick Confirm and Execute PHP Code

Post by jkwok »

Hi,

I have a tool bar consisting of links. One link in particular is to delete a database item. Is there a way to have a confirmation window pop up when the user clicks on the delete link, and if they confirm deletion then the necessary PHP code can be called on to perform the work?

This link exists on a page that displays the database item. So if they click delete, they must confirm. When confirmed, the deletion is performed and the page is redirected.

Here is what the link looks like currently:

Code: Select all

<a href="" onClick="confirmation(<?php echo $_GET['id']; ?>); return false;">Delete</a>
Here is the confirmation() function:

Code: Select all

 
<script type="text/javascript">
<!--
  function confirmation(ID)
  {
    var answer = confirm("Are you sure you want to delete this article?")
    if (answer)
    {
      window.location = "news.php";
    }
  }
//-->
</script>
 
Currently, all this done is simply redirect when confirmed because I'm doubtful on how PHP can be embedded into javascript like this, so I didn't bother including any PHP code. If this isn't possible, I'm open to suggestions!

Any help would be greatly appreciated!
Last edited by Benjamin on Fri May 29, 2009 10:52 am, edited 1 time in total.
Reason: Changed code type from text to javascript.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: onClick Confirm and Execute PHP Code

Post by kaszu »

Attilitus
Forum Commoner
Posts: 27
Joined: Wed Aug 08, 2007 2:32 pm

Re: onClick Confirm and Execute PHP Code

Post by Attilitus »

Here are some demos that will help. http://demos.mootools.net/

Look at HTTP Request and Form.send you should be able to implement what you want with some very minor editing of those demos. (Although investing a little time in becoming comfortable with mootools would be well worth the effort.)
Post Reply