<a href="javascript:delete_signup({$action.action_id})" class="btn btn-danger btn-xs">取消報名</a>
接著修改 index.php 的 list_action 函數中利用 tadtools內建的 sweet_alert 類別再產生一組刪除的js函數,以便做出「確認後刪除的功能」:
//顯示活動列表
function list_action()
{
global $xoopsDB, $xoopsTpl, $isAdmin;
$tbl = $xoopsDB->prefix('actions');
$where = $isAdmin ? '' : "WHERE `enable` ='1' AND `end_date` > now() ";
$sql = "SELECT * FROM `{$tbl}` $where ORDER BY `end_date` DESC";
//getPageBar($原sql語法, 每頁顯示幾筆資料, 最多顯示幾個頁數選項);
$PageBar = getPageBar($sql, 5, 10);
$bar = $PageBar['bar'];
$sql = $PageBar['sql'];
$total = $PageBar['total'];
$xoopsTpl->assign('bar', $bar);
$xoopsTpl->assign('total', $total);
$result = $xoopsDB->query($sql) or web_error($sql);
$myts = MyTextSanitizer::getInstance();
while ($action = $xoopsDB->fetchArray($result)) {
$action['title'] = $myts->htmlSpecialChars($action['title']);
$action['content'] = $myts->displayTarea($action['content'], 1, 1, 1, 1, 0);
$actions[] = $action;
}
$xoopsTpl->assign('actions', $actions);
include_once XOOPS_ROOT_PATH . "/modules/tadtools/sweet_alert.php";
$sweet_alert = new sweet_alert();
$sweet_alert->render("delete_action", "admin/main.php?op=delete_action&action_id=", 'action_id');
$sweet_alert2 = new sweet_alert();
$sweet_alert2->render("delete_signup", "index.php?op=delete_signup&action_id=", 'action_id', "確定要取消嗎?" , "取消後就不能參加活動囉!" , "是!含淚取消!");
}
case "delete_signup":
delete_signup($action_id);
header("location:{$_SERVER['PHP_SELF']}?action_id=$action_id");
exit;
接著實際做出該函數:
//取消報名
function delete_signup($action_id)
{
global $xoopsDB, $xoopsUser;
$uid = $xoopsUser->uid();
$tbl = $xoopsDB->prefix('signups');
$sql = "DELETE FROM `{$tbl}` WHERE `action_id`='{$action_id}' and `uid`='{$uid}'";
$xoopsDB->queryF($sql) or web_error($sql);
$_SESSION['uid_signup'] = array_diff($_SESSION['uid_signup'], [$action_id]);
}