Hey guys,
I have been searching for this for quite a while and tried few methods but its nor working. I am a newbie in php and will be glad for you help.


The code below displays a page that lists all the programs in the database and then I will be providing functions such as adding, delete selected, modify selected options. This code is not fully complete for other options.

This page calls the example4.php where I have created cases for the action called. The problem I have is when an user selects checkboxes and click delete selected it should just display all the selected checkboxes and then click delete if he wants to proceed.
I am able to display the selected checkboxes but I can't understand how can I delete them.
I read something on using onclick calling a javascript function and then redirecting it to another case on the same php page, but I don't know how I can pass selected values to the javascript or the new case.

I tried deleting the checkboxes directly without using the confirmation page but I was really interested to come up with a way to display the selected checkboxes and then delete them.

<form  method="post" action="example4.php" >
  <?php
if(empty($_POST["check"]))
{
 $conn = oci_connect("","","");
	
	
    $query="SELECT * FROM PROGRAMS";
    $stmt=oci_parse($conn, $query);
	
	oci_execute($stmt);

 ?>
   
    <div id = "menu">
      <table>
        
        
        
        
        
        <tr>
          <td><input type="submit" value="Delete Selected" formaction="example4.php?action=confirmdelete" " style="width: 200px;" /></td>
        </tr>
        <tr>
          <td></td>
        </tr>
      </table>
    </div>
    <div id ="table">
   <center> <h1>Program Details </h1></center>
    <table border="1" align="center" >
  <tr>
        <th>PROGRAM NAME</th>
        <th>PROGRAM CONTACT NAME</th>
        <th>DETAILS</th>  
        <th colspan="2">SELECT</th>      
    </tr>
<?php
      while ($row = oci_fetch_array ($stmt))
    {
 ?>
       <tr>
       <td><?php echo $row["PROGRAM_NAME"]; ?></td>
       <td><?php echo $row["PROGRAM_CONTACT"]; ?></td>
       <td><?php echo $row["PROGRAM_CONTACT_DETAIL"]; ?></td>  
	   <td><center><input type="checkbox" name="check[]" value="<?php echo $row["PROGRAM_NAME"]; ?>"></center></td>

       </tr>
<?php
    }
 ?>
    </table></div>
</form>
    

<?php
   oci_free_statement($stmt);
  oci_close($conn);
}
 ?>
</body>
</html>

this is the example4.php page which act as a processing page

<script language="javascript">
 function confirm_delete(){
   window.location="example4.php?action=delete";
   }
</script>

<?php
$function = $_GET["action"];
switch($function)
		{
		case "delete":
		
    
		foreach($_POST["check"] as $name)
    {
		$conn = oci_connect("","","");
      $query = "DELETE FROM PROGRAMS 
        WHERE PROGRAM_NAME ='".$name."'";
      $stmt = oci_parse($conn,$query);
      oci_execute($stmt);
      echo " deleted";
       
}

       

break;


case "confirmdelete":
?>
Confirm deletion of the customer record
<table border="1" align="center">
    <tr>
        <th>PROGRAM NAME</th>
        <th>PROGRAM CONTACT NAME</th>
        <th>DETAILS</th>  
        
    </tr>
    <?php
		
	 foreach($_POST["check"] as $name)
	 {
    $conn = oci_connect("","","");
      
	  $query = "SELECT * FROM PROGRAMS WHERE PROGRAM_NAME='".$name."'";
    $stmt = oci_parse($conn,$query);
    oci_execute($stmt);
     while ($row = oci_fetch_array ($stmt))
	 {
		 ?>
    <tr>
        <td><?php echo $row["PROGRAM_NAME"];  ?></td>
        <td><?php echo $row["PROGRAM_CONTACT"];  ?></td>
        <td><?php echo $row["PROGRAM_CONTACT_DETAIL"];  ?></td>
        </tr>
        <?php
	 }
	 
	 }
	 ?>
    </table>
	  <p />
	  
	 <center><input type="submit"  value="delete" onClick="confirm_delete($name)"/></center>
     
     
     
	<center>  <input type="reset" value="Clear Details" /></center>
    
	  
	  
      <?php
	  break;
		}
	  
	  oci_free_statement($stmt);		 
  
		
  ?>

Please do ignore the fact that I have used the connection seperately in the cases as I know that's a wrong practise but I have been pulling code from different pages to see if it works.

Member Avatar
diafol

Confirmation page makes things more difficult as you have to pass POST data to the delete page. I'm assuming this setup:

form page -> confirm delete page -> delete from db page

I'd advise using session var to pass the data to the last page.

form page

session_start();
if(isset($_SESSION['post']))unset($_SESSION['post']);

confirm page

session_start();
if(isset($_POST))$_SESSION['post'] = $_POST;

delete from page

session_start();
if(isset($_SESSION['post'])){
  //do the delete
  //after data validation, I suggest you use an IN clause as opposed to running individual queries for each checked program
  //DELETE FROM programs WHERE id IN (1,6,9,12,15,34,78) 
  //the id values can be built into a list via implode()
}

Just an idea. Perhaps you'd find it easier to do a js confirm, but you'll be aware of the problems when you RELY on js for functionality.