how to set up delete confirmation in php


I am trying to set up delete confirmation for my multiple-select delete checkboxes. Any ideas?

<?php if(isset($_SESSION["username"]))

 // start -> delete any valid checked rows ... this is for the multiple-select delete
     if ( isset ( $_POST['option'] ) )
          $temp = array ();
          foreach ( $_POST['option'] AS $id )
               $id = intval ( $id );
               if ( $id > 0 )
                    $temp[] = 'listingid = ' . $id;
          if ( ! empty ( $temp ) )
               $sql = "DELETE FROM listings WHERE " . implode ( ' OR ', $temp );
               mysql_query ( $sql );
               unset ( $id );
          unset ( $temp );
     // end -> delete any valid checked rows... this is for the multiple-select delete
      // Make the query.
      $query = "SELECT customers.*, Listings.* FROM customers INNER JOIN listings ON customers.customerid =  listings.customerid WHERE customers.customerid = " .$_SESSION['customerid']. " ORDER BY listings.$order_by LIMIT $start, $display";
      $result = @mysql_query ($query); // Run the query.
      $num = mysql_num_rows ($result); // How many listings are there?
// Table header.
echo' <form name="form1" method="post" action='.$_SERVER["PHP_SELF"];
echo '><table align="center" cellspacing="0" cellpadding="5" width="100%" class="gridtable">
<tr class="header" align="left">
      <td align="center" class="editanddelete" width="5%" valign="top">Edit</td>
      <td align="center" class="editanddelete" width="5%" valign="top">Delete</td>
      <td align="left" width="5%" valign="top"><a href="' . $link5 . '" class="gridheader">Photo?</a>' . $sortimg5 . '</td>
      <td align="left" width="25%" valign="top"><a href="' . $link1 . '" class="gridheader">Title</a>' . $sortimg1 . '</td>
      <td align="left" width="40%" valign="top"><a href="' . $link2 . '" class="gridheader">Description</a>' . $sortimg2 . '</td>
      <td align="left" width="15%" valign="top"><a href="' . $link3 . '" class="gridheader">Price</a>' . $sortimg3 . '</td>
      <td align="left" width="5%" valign="top"><a href="' . $link4 . '" class="gridheader">Display?</a>' . $sortimg4 . '</td>
// Fetch and print all the records.
$bg = '#eeeeee'; // Set the background color.
$edit = 'icon_edit_graybg.gif'; // Set the Edit image
$delete = 'icon_delete_graybg.gif'; // Set the Delete image

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
      $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.
      $edit = ($edit=='icon_edit_graybg.gif' ? 'icon_edit_whitebg.gif' : 'icon_edit_graybg.gif'); // Switch the Edit image.
      $delete = ($edit=='icon_delete_graybg.gif' ? 'icon_delete_whitebg.gif' : 'icon_delete_graybg.gif'); // Switch the Delete image.

      echo '<tr bgcolor="' . $bg . '">
            <td align="center"><a href="edit_listing.php?id=' . $row['listingid'] . '"><img src="../images/' . $edit . '"></a></td>
            <td align="center"><input type="checkbox" name="option[]" value="' . intval( $row['listingid'] ) . '"></td>
            <td align="left">' . ( $row['photo_exists'] == 1 ? '<img src="../images/icon_camera.gif">' : '' ). '</td>
            <td align="left">' . $row['title'] . '</td>
            <td align="left">' . $row['description'] . '</td>
            <td align="left">' . $row['price'] . '</td>
            <td align="left">' . ( $row['display'] == 1 ? 'Yes' : 'No' ). '</td>
            echo '</table><p><input type="submit" value="Delete Checked" class="button" /></p></form>'; // Close the table.

            mysql_free_result ($result); // Free up the resources.      
      } else { // If there are no registered users.
            echo '<span class="darkbluebig">There are currently no listings.</span>';
      mysql_close(); // Close the database connection.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Right when you execute your query:

               $sql = "DELETE FROM listings WHERE " . implode ( ' OR ', $temp );
               $result = mysql_query ( $sql );
               if ($result)
                   echo '<h1>Deleted Items Successfully</h1>';
                   echo '<h1>Delete Failed</h1>';
               unset ( $id );

The only real big change is:
$result = mysql_query($sql);
mysql_query() returns false if it failed, so if $result will work everytime your query does not fail.

Joe P
stevegingellAuthor Commented:
Hi again Joe....i should have framed the Q differently. By confirmation i meant a popup( javascript i am guessing) which warns the user that they are about to delete listings. so u can either hit OK or cancel.
Hehe, ok.
There is a confirm() javascript method that is a popup with ok/cancel.

A simple example would be: (in javascript)
var bool = confirm("Are you sure that you want to delete the selected listings?");
if (bool) { } // they said OK
else { } // They said CANCEL

When they push a button I am guessing this happens so you can put it all in onClick()
<input type="submit" value="Delete" onClick="return confirm('Are you sure that you want to delete the selected listings?');">

That is it all condensed into a return.  If they clicked Cancel, confirm returns false and the onClick will return false and it should do nothing.
If they clicked Okay, confirm returns true and the onClick will return true and the button will act normally.

Joe P

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

I found the line I was looking for,

Try changing:
echo '</table><p><input type="submit" value="Delete Checked" class="button" /></p></form>'; // Close the table.

echo '</table><p><input type="submit" value="Delete Checked" class="button" onClick="return confirm(\'Are you sure that you want to delete the selected listings?\');" /></p></form>';

Notice now that it is in php I needed to escape the single quotes.
Joe P
stevegingellAuthor Commented:
Thanks much!
No problem =)
Joe P
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.