我正在使用MySQL表格内容创建表格。问题是我正在尝试为表中的每一行创建一个删除(x)按钮(删除列)。
<?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;
我尝试使用onclick按钮,但绝对失败了。任何人都可以给我任何提示吗?
答案 0 :(得分:0)
我的解决方案只是众多可能的解决方案之一
1)创建js函数以删除行并调用ajax请求以从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)创建按钮
$html .= '<td><a href="javascript:removeRow(this,'.$e->eventId.');return false;">Delete</a></td>';
3)创建PHP脚本
<?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();