SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot maffp's Avatar
    Join Date
    Jun 2005
    Location
    UK
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to do a Confirmation popup for deleting of MySQL database entry???

    Hi!

    I'm wanting to make a confirmation popup for when I click to delete an author from a database which either continues running the script and deletes the author (YES) or cancels the script and goes back (NO).

    The php files are here:

    authors.php
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <html>
    <head>
    <title>Joke CMS: Manage Authors</title>
    <meta http-equiv="content-type"
        content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <h1>Manage Authors</h1>
    <ul>
    <?php

    $dbcnx 
    = @mysql_connect('localhost''root''mypasswd');
    if (!
    $dbcnx) {
      exit(
    '<p>Unable to connect to the ' .
          
    'database server at this time.</p>');
    }

    if (!@
    mysql_select_db('ijdb')) {
      exit(
    '<p>Unable to locate the joke ' .
          
    'database at this time.</p>');
    }

    $authors = @mysql_query('SELECT id, name FROM author');
    if (!
    $authors) {
      exit(
    '<p>Error retrieving authors from database!<br />'.
          
    'Error: ' mysql_error() . '</p>');
    }

    while (
    $author mysql_fetch_array($authors)) {
      
    $id $author['id'];
      
    $name htmlspecialchars($author['name']);
      echo 
    "<li>$name ".
          
    "<a href='editauthor.php?id=$id'>Edit</a> ".
          
    "<a href='deleteauthor.php?id=$id'>Delete</a></li>";
    }

    ?>
    </ul>
    <p><a href="newauthor.php">Add new author</a></p>
    <p><a href="index.html">Return to front page</a></p>
    </body>
    </html>

    deleteauthor.php
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Joke CMS: Delete Author</title>
    <meta http-equiv="content-type"
        content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <?php

    $dbcnx 
    = @mysql_connect('localhost''root''mypasswd');
    if (!
    $dbcnx) {
      exit(
    '<p>Unable to connect to the ' .
          
    'database server at this time.</p>');
    }

    if (!@
    mysql_select_db('ijdb')) {
      exit(
    '<p>Unable to locate the joke ' .
          
    'database at this time.</p>');
    }

    // Delete all jokes belonging to the author
    // along with the entry for the author.
    $id $_GET['id'];
    $ok1 = @mysql_query("DELETE FROM joke WHERE authorid='$id'");
    $ok2 = @mysql_query("DELETE FROM author WHERE id='$id'");
    if (
    $ok1 and $ok2) {
      echo 
    '<p>Author deleted successfully!</p>';
    } else {
      echo 
    '<p>Error deleting author from database!<br />'.
          
    'Error: ' mysql_error() . '</p>';
    }

    ?>
    <p><a href="authors.php">Return to authors list</a></p>
    </body>
    </html>
    Thanks.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,270
    Mentioned
    197 Post(s)
    Tagged
    3 Thread(s)

    confirm delete

    You could modify the authors.php file, change this line
    PHP Code:
          "<a href='deleteauthor.php?id=$id'>Delete</a></li>"
    to something like
    PHP Code:
          "<a href='' onclick='confirmDelete('" $id "'>Delete</a></li>"
    Then put a confirmDelete function in the head (or external file) something like (this is in rough pseudo-code)
    HTML Code:
    function confirmDelete(id_value){
      confirm("Are you sure?"){
    /* if yes */
        document.location.href = deleteauthor.php?id=id_value;
      }else{
      return;
      }
    }


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
  •