I'm using to create a table using MySQL table contents. The problem is I'm trying to create a delete (x) button for each row in the table (Delete column).
<?php
$html = "<table>";
$html .= "<tr>";
$html .= "<th>id</th>";
$html .= "<th>Event type</th>";
$html .= "<th>Date</th>";
$html .= "<th>Price(€)</th>";
$html .= "<th>Description</th>";
$html .= "<th>Delete</th>";
$html .= "</tr>";
foreach($event as $e){
$html .= "<tr>";
$html .= "<td>$e->eventId</td>";
$html .= "<td>$e->eventType</td>";
$html .= "<td>$e->eventDate</td>";
$html .= "<td>$e->eventPrice</td>";
$html .= "<td>$e->eventDescr</td>";
$html .= "<td><--the delete button has to be here--></td>";
$html .= "</tr>";
}
$html .= "</table>";
echo $html;
I've tried using a button onclick but absolutely failed. Could anyone give me any tips?
My solution is only one of many possible
1) create js function to remove row and call an ajax request to delete an item from DB
function removeRow(item,eventId)
{
var row = $(item).parent().parent();
$.ajax(url: '(url_to)/remove.php?id='+eventId,
success: function(resp)
{
if(resp=='OK')
{
row.remove();
}
}
);
}
2) create button
$html .= '<td><a href="javascript:removeRow(this,'.$e->eventId.');return false;">Delete</a></td>';
3) create PHP script
<?php
//connection to DB
$dbhost = "localhost";
$dbname = "dbb";
$dbuser = " ";
$dbpswd = " ";
try
{
$db = new PDO("mysql:host=".$dbhost.";dbname=".$dbname,$dbuser,$dbpswd);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//removing event
$count = $db->exec("DELETE FROM `someTable` WHERE id = {intval($id)}");
//return OK if event was correctly deleted
if($count==1)
{
echo 'OK';
}
}
catch (PDOException $e)
{
//only for development version
echo $e->getMessage();
}
die();