我有一个显示通知的表,并且每行旁边都有一个清除选项。但是,当我按清除按钮时,什么也没有发生。它当前使用一个旨在触发清除功能的按钮运行。有什么问题吗?
<button id="'.$row[0].'" class="btn-link" style="color: red;" value="'.$row[0].'" onclick="clearNotice('.$row[0].')">Clear</button>
function clearNotice($NoticeId)
{
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$link) {
die('Could not connect: ' .mysql_error());
}
//First delete from calls list
$query = "DELETE FROM notices WHERE id = ?";
try {
$stmt = mysqli_prepare($link, $query);
mysqli_stmt_bind_param($stmt, "i", $NoticeId);
$result = mysqli_stmt_execute($stmt);
if ($result == FALSE) {
die(mysqli_error($link));
}
}
catch (Exception $e)
{
die("Failed to run query: " . $e->getMessage());
}
}
答案 0 :(得分:-1)
PHP是服务器端。您将需要执行以下操作:
<?php
function clearNotice($NoticeId)
{
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$link) {
die('Could not connect: ' .mysql_error());
}
//First delete from calls list
$query = "DELETE FROM notices WHERE id = ?";
try {
$stmt = mysqli_prepare($link, $query);
mysqli_stmt_bind_param($stmt, "i", $NoticeId);
$result = mysqli_stmt_execute($stmt);
if ($result == FALSE) {
die(mysqli_error($link));
}
}
catch (Exception $e)
{
die("Failed to run query: " . $e->getMessage());
}
}
if(isset($_POST) && isset($_POST['delete-me'])){
clearNotice($_POST['delete-me']);
}
// do whatever you do to get $row
?>
<form action="" method="post">
<button name="delete-me" id="delete-<?php echo $row[0]; ?>" class="btn-link" style="color: red;" value="<?php echo $row[0] ?>">Clear</button>
</form>
还请注意ID的更改,因为您不能用数字开头ID(假设ID是数字)
id="delete-<?php echo $row[0]; ?>"