SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    confirm a deletion action

    To confirm that a CMS user really wants to delete a record, I have added some Java Script that was originally intended to confirm if a user wants to visit a new web page that takes a long time to download. The script provided two location.replace() options: 1) to visit the new page or 2) to return to the previous page. I want to modify it so that the options are 1) to continue on to the following deletion script or 2) reurn to the previous page. How do I set up option 1) to do this in the following script? Probably something else than location.replace().

    Right now I have option 1) set to direct the user to the same deletion page where this script resides. But this will just loop the user back thru the same JavaScript. Again, I want the first option to just allow the user to continue on thru the rest of the deletion script.
    Code:
     
    var msg = "\nYou are about to delete this resource.\n\n" +
    	 "Is this really what you want to do?":
     
    if (confirm(msg))
    //option 1
    location.replace(Reservations/cms/resources_delete.php);
     
    else 
    //option 2
    location.replace(Reservations/cms/resources_view.php);
     
    //The rest of the deletion script
    // Delete all resource lookup entries for the
    // resource along with the entry for the resource.
    $ok1 = @mysql_query("DELETE FROM RsrcLookup WHERE RsrcID=$id"); 
    $ok2 = @mysql_query("DELETE FROM Rsrc WHERE ID=$id");
    if ($ok1 and $ok2) {
    echo("<p>Resource deleted successfully!</p>");
    } else {
    echo("<p>Error deleting resource from database!<br />".
    	 "Error: " . mysql_error() . "</p>");
    }
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe move the deletion script to the onsubmit event of the page before (which I assume has a form), like:

    Code:
    function doConfirm()
    {
    if (confirm(msg))
      return true;
    else
      return false;
    }
    ...
    <form method="post" action="restofdeletionscript.php" onsubmit="return doConfirm();">

  3. #3
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The previous page points to the deletion script via a url:

    HTML Code:
     <a href='Reservations/cms/resources_delete.php?id=$id'>Delete</a>
    If I go with the approach you suggested, how would I modify the url?

    It is part of a PHP while loop as follows:

    PHP Code:
     $resources mysql_query($select $from $where);
    echo (
    "<p>SQL: $resources</p>" );
    if (!
    $resources) {
    echo(
    "</table>");
    echo(
    "<p>Error retrieving resources from database!<br />".
    "Error: " mysql_error() . "</p>");
    exit();
    }
    while (
    $resource mysql_fetch_array($resources)) {
    echo(
    "<tr valign='top'>\n");
    $id    $resource["ID"];
    $resourcetitle $resource["RsrcTitle"];
    $new_resourcetext htmlspecialchars($resource["new_RsrcText"]);
    $resourcedate htmlspecialchars($resource["RsrcDate"]);
    $metatitle $resource["MetaTitle"];
    $metadescrip $resource["MetaDescrip"];
    $metakeywords $resource["MetaKeywords"];
    echo(
    "<td>$metatitle</td>\n");
    echo(
    "<td>$metadescrip</td>\n");
    echo(
    "<td>$metakeywords</td>\n");
    echo(
    "<td>$resourcetitle</td>\n");
    echo(
    "<td>$new_resourcetext</td>\n");
    echo(
    "<td>$resourcedate</td>\n");
    echo(
    "<td>[<a href='Reservations/cms/resources_edit.php?id=$id'>Edit</a>]"
    );
    echo(
    "<br /><br />[<a href='Reservations/cms/resources_delete.php?id=$id'>Delete</a>]<br /><br />".
     
        
    "</td>\n");
    echo(
    "</tr>\n");

    Paul C.
    ClickBasics
    http://www.clickbasics.com

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe something like
    Code:
    <script language="JavaScript">
    function ConfirmDelete(id)
    {
      if( confirm( "Really want to delete this?" ) ){
        location.href = "Reservations/cms/resources_delete.php?id=" + id
      }
    }
    </script>
    ....
    <a href="javascript:ConfirmDelete('$id')">Delete</a>
    I assume that sticking that $id in there would replace it with the right value when the page generates, Im no php guru though.

  5. #5
    SitePoint Zealot y2kbug's Avatar
    Join Date
    Nov 2004
    Location
    Hong Kong
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wanna ask a question.

    I am using this:
    PHP Code:
    <script language="JavaScript">
    function 
    ConfirmDelete(id)
    {
      if( 
    confirm"Really want to delete this?" ) ){
        
    location.href "Reservations/cms/resources_delete.php?id=" id
      
    }
    }
    </script>
    ....
    <a href="javascript:ConfirmDelete('$id')">Delete</a> 
    After I have clicked on "Delete", a message box popped up.
    At the same time, the background music stopped!
    After I have chosen "No", there was still no music...
    Why this happened??

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Weird, maybe because you're hitting a link regardless of whether it navigates away.

    <a href="#" onclick="javascript:ConfirmDelete('$id')">Delete</a>, that should work.

  7. #7
    SitePoint Zealot y2kbug's Avatar
    Join Date
    Nov 2004
    Location
    Hong Kong
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That works!
    Thank you~


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •