我的Web应用程序上有一个搜索页,可在数据库的事件表中搜索事件。我想拥有它,以便在搜索结果的末尾有一个删除按钮,可以删除数据库条目,还可以在它们删除数据之前弹出一个警告框。
我的搜索结果显示如下代码:
if(mysqli_num_rows($result) > 0) {
echo "<br>Result Found: ";
echo "<br><table>";
While($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>Event Number: " . $row['EventID'] . "</td>";
echo "</tr><tr>";
echo "<td>Location: " . $row['Location'] . "</td>";
echo "</tr><tr>";
echo "<td><input type='button' name='Delete' value='Delete' onClick='getConfirmation();'></td>";
}
echo "</table
我不太确定要在javascript函数中放什么。我希望它出现一个基本的警告框,如果用户单击“确定”按钮,则该动作将为“ delete.php”,理想情况下,还会出现另一个弹出窗口,提示“记录已成功删除”。我还需要确保从记录中获取eventID,以便可以成功删除它。到目前为止,对于JS函数,我有:
<script type = "text/javascript">
<!--
function getConfirmation() {
var retVal = confirm("Are you sure you would like to delete this record?");
if( retVal == true ) {
delete.php
document.write("Record deleted successfully");
}
else {
return false;
}
}
//-->
</script>
我知道这是错误的,但是我想知道如何解决此问题? 我还可以说一下,如果他们按是,则执行php而不是删除delete.php?
答案 0 :(得分:0)
要实现此目的,您需要在onclick事件中传递eventId,如下所示:
if(mysqli_num_rows($result) > 0) {
echo "<br>Result Found: ";
echo "<br><table>";
While($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>Event Number: " . $row['EventID'] . "</td>";
echo "</tr><tr>";
echo "<td>Location: " . $row['Location'] . "</td>";
echo "</tr><tr>";
echo "<td><input type='button' name='Delete' value='Delete' onClick='getConfirmation(".$row['EventID'].");'></td>";
}
echo "</table>";
以及您的JS代码中,您需要: 1.在您的js函数中获取eventId。 2.调用ajax删除该事件。 3.
之后显示成功警报 <script type = "text/javascript">
function getConfirmation(eventId) {
var retVal = confirm("Are you sure you would like to delete this record?");
if( retVal == true ) {
$.ajax({
url: "delete.php",
method: "POST",
data: {eventId: eventId},
success: function (response) {
if(response.status) {
alert("Record Deleted Successfully");
}
}
});
} else {
return false;
}
}
</script>
此外,在您的delete.php文件中,您需要通过$ _POST ['eventId']获取eventId,并且可以执行删除查询。您需要发回状态。希望对您有帮助。