我使用锚标记运行JS函数confirm(),如果用户单击okay,则添加“deltopic = id”,并使用$ _GET方法获取'deltopic'来删除该特定项,但是似乎没有找到$ _GET ['deltopic']
<script language="JavaScript" type="text>
function deltopic(title, tid) {
if(confirm("Are you sure you want to delete '" + title + "'")){
window.location.href = "?viewtopic.php&deltopic=" + tid;
}
}
</script>
<?php
if(isset($_GET['deltopic'])){
if($_GET['deltopic'] !=='1'){
$query = "DELETE FROM `bkg`.`bkg_topics` WHERE `bkg_topics`.`topic_id` = :topicid";
$query_params = array(':topicid' => $_GET['deltopic']);
try{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
header('Location: index.php?forums&action=deleted');
exit;
} catch(PDOException $e) {
$error[] = "An error has occured. Please try again later.";
}
}
echo "deltopic is set";
}
我添加了最后一个回显只是为了查看它是否设置了deltopic
,或者我的SQL中是否出现了错误。但是我没有看到“设置deltopic”。
我不确定我做错了什么和/或我忘了什么。我有类似的代码,它确实有效,并仔细检查它。
编辑:我在'window.location.href'字符串中看到了错误,我将.php添加到了?viewtopic的末尾,使其成为?viewtopic.php
。删除.php也解决了我的问题。
答案 0 :(得分:1)
window.location.href = "?viewtopic.php&deltopic=" + tid;
将导致URL“viewtopic.php”是URL QUERY 的一部分。也许你想要它在 PATH :
window.location.href = "viewtopic.php?deltopic=" + tid;