0

i have problem to delete a data using checkbox ..please help me
this's coding

dataprocessor.php

<?php


@ $db = mysql_pconnect('localhost','root','');
if (!$db)

    echo'Error: no connection database. Cuba.';
    exit;
}

mysql_select_db('profil1');
$query = "select * from tblprocessor order by Name ";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
echo "<center>";


echo "<table border='1' cellpadding='5' cellspacing='8'>";
   echo" 
   <tr> <td >Bil</td> <td>Name</td> <td><input  type='checkbox' id='delete' value='delete'  title='select or deselect all massage'></td> </tr>";


   for ($i=0; $i <$num_results; $i++)
{
 $row = mysql_fetch_array($result);
 $Name= $row["Name"];
     //fetch the result from query into an array
echo "
<tr> <td>".($i+1)."</td> <td>".$row['Name']."</td> <td><input name='delete' type='checkbox' id='delete' value=",$Name,"></td> <td align=center width=10><a href='deleteprocessor.php?Name=$Name'> Delete </a></td> </tr>";
}
 echo "</table>";
 echo"<button><a href='deleteprocessor.php'>Delete</a></button>"
 ;
  ?>

this's coding
deleteprocessor.php

<?php
include '../dbase.php';

if(isset($_POST['delete'])) {
foreach($_POST['checkbox'] as $Name)
{
$query=mysql_query ("DELETE FROM tblprocessor WHERE Name='.$Name.'");

}

if (($query)){
    echo "<meta http-equiv='Refresh' content='1; url=dataprocessor.php'>\n";
    mysql_close($conn); 
    } } 
    ?>
2
Contributors
2
Replies
12
Views
1 Year
Discussion Span
Last Post by dizy
0

Well to start,
when you echo out your input checkbox, don't set id="delete". This will give all your inputs the same id of "delete"
and that is bad html code.. If you want to assign delete to all you checkbox input's use a class

So change: <tr><td>Bil</td><td>Name</td><td><input id='delete' value='delete' title="select or deselect all message'></td></tr>
To:
<tr><td>Bil</td><td>Name</td><td><input class='delete' value='delete' title="select or deselect all message'></td></tr>

This will set your html correct.
To take this functionality further, you can use ajax to run your delete statment.
First, you will need to add a unique identifier foreach checkbox that is outputted to the screen. The record id from the database would be a good id.

Like so:
<tr><td>Bil</td><td>Name</td><td><input id='". $row['id'] ."' class='delete' value='delete' title="select or deselect all message'></td></tr>
This gives you a target for ajax to send back to your delete script.
I use jQuery.

$('.delete').on('click', function(e){
    e.preventDefault();
    var id = $(this).attr('id'); // Get the id of the checkbox input
    $.ajax({
    url: 'deletescript.php',
    type: 'POST',
    data: 'id: id',
    success: function(data){
    alert('Record has been deleted');
    }
    });
});

You may have to play around with the event being fired.. "click" or "change" event will need to be used.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.