Reputation: 55
I can successfully extract the data from the database however am having trouble now deleting a specific row using the checkbox method.
I have used a number of different tutorials including previous posts here but it's still not there.
Here is my code:
<div id="content">
<h1>Post Bulletin</h1>
<br />
<form action="insert.php" method="post">
<label for="title">Title</label>
<br />
<input type="title" name="title" id="file" required="required" placeholder="Bulletin Title" />
<br />
<br />
<label for="bulletin">Bulletin</label>
<br />
<textarea id="bulletin" name="bulletin" id="bulletin" rows="10" cols="50" required="required" placeholder="Bulletin Message"></textarea>
<br />
<input type="submit" />
</form>
<br/ >
<br/ >
<h3>Current Bulletin Posts</h3>
<?php
require('connect.php');
$data = mysql_query("SELECT * FROM bulletins ORDER BY Date_Entered DESC")
OR die("Can't access bulletins table. " . "<br />". mysql_error());
while($info = mysql_fetch_array($data))
{
Echo "<br />" . "Delete: " . "<input name='checkbox[]' type='checkbox' id='checkbox'/>" . "<br />" . "<br />";
Echo $info['Title'] . "<br />" . "<br />";
Echo $info['Bulletin'] . "<br />" . "<br />";
Echo "Posted at " . $info['Date_Entered'] . "<br />" . "<br />";
Echo "<hr>";
}
?>
I have created the checkbox but am unable to process the deletion.
Am I right in thinking that a 'if isset' statement would suffice? How would I assign a ID to the array of checkbox?
Upvotes: 0
Views: 1000
Reputation: 55
You can use EXTJS to show the fetched records from Database, and then by just adding below code you can delete all the selected checkbox rows.
function delMethod(index)
{
try{
var rowstoDel=new Array();
var count=0;
var recordsToDelete = new Array();
for (var i=0; i <store2.getCount(); i++){
var rec=store2.getAt(i);
if (rec.get("MappingGrid")==index){
recordsToDelete[recordsToDelete.length] = rec;
rowstoDel[count]=i;
count++;
}
}
for (var i=0; i <recordsToDelete.length; i++){
store2.remove(recordsToDelete[i]);
}
for (var j=rowstoDel.length-1;j>=0;j=j-1)
{
myData2.splice(rowstoDel[j],1);
}
}catch(e){alert("unable to delete")}
}
Upvotes: 0
Reputation: 64526
Use the row ID as the checkbox's value
, this is what will be submitted to the PHP side.
Echo "<br />" . "Delete: " . "<input name='delete[]' type='checkbox' value='" . $info['id'] . "' />" . "<br />" . "<br />";
Now, on the PHP side, you will receive the record ID's to delete as an array in $_POST['delete']
.
Also your checkboxes must be contained within a form for them to be submitted.
Upvotes: 1
Reputation: 2630
Firstly, Checkboxes should be in tag.
After that you need to set the value
attribute to checkbox which will be the unique key for that checkbox as well as the unique key for the table row.
If you are using php to delete the data then you just need to go through the selected checkbox array and fire the delete query for every array element(i.e nothing but your table unique key).
Upvotes: 0