Jump to content

delete row using checkbox


noobdood

Recommended Posts

Hi guys! :) im new to phpfreaks *waving*

 

im trying to delete row from a table that is populated by data from database using checkbox(and it doesnt matter if its just one checkbox or multiple) but its not working, as in nothing happens. no delete, no errors or warnings, just a refresh.

 

table:

<form class="event-button-wrap" method="post" action="event.php">
    <div class="button-wrap">
    <input type="submit" id="del_event" name="del_event" value="Delete Event"/>
    </div>
	
    <div class="event-list-table2" >
      <table class="event-table">
	<thead>
	<tr>
          <th>Event Name</th>
	  <th class="head">Event Date</th>
	  <th class="head">Venue</th>
	</tr>
	</thead>
	<tbody class="tbody-event-list-table2">
	<?php
	require "connection.php";
	$check = mysql_query("SELECT * FROM event");
	if(mysql_num_rows($check) > 0)
	{
	  while($row = mysql_fetch_array($check))
	  {
	  $id = $row['event_id'];
          $name = $row['event_name'];
	  $dstart = $row['start_date'];
	  $venue = $row['event_venue']; 						          
          echo 
	  "<tr>
	     <td>
              <input type='checkbox' name='check[]' class='check' value='$id'>$name
             </td>
	  </tr>";
	  }
         }	
	 else
	 {
	  echo 
	  "<tr>
	     <td colspan='4'>There Are No Events.</td>
	   </tr>";
	  }
         ?>
    </tbody>
</table>
</div>
</form>

process is before <!DOCTYPE html>:

<?php
   if(isset($_POST['del_event']))
   {
     if(isset($_POST['check']))
     {
       foreach($_POST['check'] as $del_id)
       {
	$del_id = (int)$del_id;
	require "connection.php";						
	$sqlqueer = mysql_query("DELETE FROM event WHERE event_id = $del_id");
	if($sqlqueer)
	{
	echo "<meta http-equiv=\"refresh\" content=\"0;URL=event.php\">";
	}
     }
    }
?>

thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/288532-delete-row-using-checkbox/
Share on other sites

<input onclick="window.href=...

 

You must trigger a jquery (javascript) mechanism to submit the form/ or open a link in order to achieve it.

 

 

https://www.google.pl/search?q=jquery+oncheck+checkbox&oq=jquery+oncheck&aqs=chrome.1.69i57j0l5.9299j0j1&sourceid=chrome&es_sm=93&ie=UTF-8

because you want to trigger an event

 

php is static on the website: it's only subjected to PARSING the sent data

 

You can do it with HTML, but would require to click SUBMIT some sort of (input).

 

 

You want to do it, without -> jquery (javascript)

 

The last, basically: listens & when the checkbox is ticked: it submits the form or directs to a link (even behind the layer of website) to do job wanted by you.

I've read the part that you want to delete a row AFTER clicking checkbox, so you need JQUERY.

 

https://www.google.pl/search?q=php+deleting+rows+checkbox&oq=php+deleting+rows+checkbox&aqs=chrome..69i57j0l2.4236j0j1&sourceid=chrome&es_sm=93&ie=UTF-8

 

It's simply to be done. http://blog.themeforest.net/tutorials/deleting-multiple-records-with-php/

 

 

1. Firstly: use 

var_export($_POST); die;

 to see what it shows/ 

 

to get what you're receiving: then, delete it in DB

 

It's simple.

Delete all the selected ids in a single query (EG ... WHERE id IN (1,2,3) ) instead of in loop.

<?php
    require "connection.php";       // only connect once, not in a loop               
    if(isset($_POST['del_event']))
    {
        if(isset($_POST['check']))
        {
            $del_ids = join(',', array_map('intval', $_POST['check']));
            $sqlqueer = mysql_query("DELETE FROM event WHERE event_id IN ($del_ids)");
            if($sqlqueer)
            {
                header("Location: event.php");
            } else {
                die ("Error in delete query");
            }
        }
    }
?>

this worked for a member on this forum of course i edited accordingly but it doesn't delete it just refreshes.

 

script:

<script>
$(document).ready(function(){
    $("#del_event").click(function()
{
        $(':checkbox:checked').each(function()
        {
$.post('delete.php', { check: $(this).attr('value') } );
        });        
});
});

</script>

 

delete.php

<?php
$del_id = $_POST['check'];
$sqlqueer = mysql_query("DELETE FROM event WHERE event_id = $del_id") or die(mysql_error());
if($sqlqueer)
{
	echo "<meta http-equiv=\"refresh\" content=\"0;URL=event.php\">";
}
?>

This works for me. Note it uses mysqli and not mysql librarybut apart from that it is fundamentally the same code.

<?php
$db = new mysqli(HOST,USERNAME,PASSWORD,'test'); // use your credentials
error_reporting(-1);

    if(isset($_POST['del_event']))
    {
        if(isset($_POST['check']))
        {
            $del_ids = join(',', array_map('intval', $_POST['check']));
            $sqlqueer = mysqli_query($db,"DELETE FROM event WHERE event_id IN ($del_ids)");
            if(!$sqlqueer)
            {
                die ("Error in delete query");
            }
        }
    }

?>
<html>
<head>
<title>Sample</title>
</head>
<body>
<form class="event-button-wrap" method="post" action="">
    <div class="button-wrap">
    <input type="submit" id="del_event" name="del_event" value="Delete Event"/>
    </div>
    
    <div class="event-list-table2" >
      <table class="event-table">
    <thead>
    <tr>
      <th>Event Name</th>
      <th class="head">Event Date</th>
      <th class="head">Venue</th>
    </tr>
    </thead>
    <tbody class="tbody-event-list-table2">
    <?php
//  require "connection.php";
    $check = mysqli_query($db,"SELECT * FROM event");
    if(mysqli_num_rows($check) > 0)
    {
      while($row = mysqli_fetch_array($check))
      {
      $id = $row['event_id'];
      $name = $row['event_name'];
      $dstart = $row['start_date'];
      $venue = $row['event_venue'];                                   
      echo "<tr>
         <td>
              <input type='checkbox' name='check[]' class='check' value='$id'>$name
             </td>
         <td>$dstart</td>
         <td>$venue</td>
         </tr>";
      }
    }    
    else
     {
      echo 
      "<tr>
         <td colspan='3'>There Are No Events.</td>
       </tr>";
     }
?>
    </tbody>
</table>
</div>
</form>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.