Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to set up delete confirmation in php

Posted on 2006-04-11
6
Medium Priority
?
330 Views
Last Modified: 2013-12-12
Hi,

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>
</tr>
';
            
// 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>
      </tr>
      ';
}
            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.
                        }
0
Comment
Question by:stevegingell
  • 4
  • 2
6 Comments
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16428242
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>';
               else
                   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
0
 

Author Comment

by:stevegingell
ID: 16428310
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.
0
 
LVL 17

Accepted Solution

by:
BogoJoker earned 1000 total points
ID: 16428397
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 17

Expert Comment

by:BogoJoker
ID: 16428415
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.

To:
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
0
 

Author Comment

by:stevegingell
ID: 16428746
Thanks much!
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16428779
No problem =)
Joe P
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question