首先,我会道歉,我对jquery / javascript的了解不是很好,所以我的错误对于某些人来说可能非常明显,但我仍然在学习,所以要谦和!!
我有一个表格,该表格允许成员从数据库中删除他们发表的帖子。表单工作正常,使用php可以根据需要从数据库中删除数据。我想创建一个弹出窗口,让用户确认这就是他们想做的事情。我不想使用特定于沼泽标准浏览器的onclick方法,我想要一些可定制的东西。因此,我尝试使用bootbox,当前在确认对话框中弹出onclick,但是当您选择yes时,则什么也没有发生。我怀疑jquery的结果没有触发表单,但是从我阅读的那部分代码来看是好的?
我已经在php,jquery和html下面粘贴了
PHP:
<?php
$deletepost = 'none';
$deleteposterror = 'none';
if(isset($_POST['delete'])) {
$del_topic_id = $_POST['topic_id_value'];
// sql to delete a record
$pass_fail = "DELETE FROM topics WHERE topic_id ='$del_topic_id';";
$pass_fail .= "DELETE FROM posts WHERE topic_id_post ='$del_topic_id'";
if (mysqli_multi_query($conn, $pass_fail)) {
$deletepost = 'show';
}else{
$deleteposterror = 'show';
echo "ERROR: Could not able to execute $pass_fail. " . mysqli_error($conn);
}
}
?>
while循环中的HTML和PHP:
<?php
$sql1 = "SELECT * FROM topics WHERE topic_by = '".$id."' ORDER BY topic_date DESC ";
$result1 = mysqli_query($conn, $sql1);
while($row = $result1->fetch_assoc()) {
$topic_id = $row ['topic_id'];
$topic_subject = $row ['topic_subject'];
?>
<div class='chat_post_titles' >
<div class='row'>
<div class='col-md-7 chat_post_text_align_left'>
<a href="../Chat/post.php?topic_id=<?php echo $topic_id?>"><?php echo $topic_subject?>.</a>
</div>
<div class='col-md-2 chat_post_text_align_right'>
<?php
$my_date = $row['topic_date'];
$date = DATE("G:i:s d/m/Y",strtotime($my_date));
?>
<div class='hide_text'>Post Date: </div>
<?php echo $date?>
</div>
<div class='col-md-2 chat_post_text_align_centre'>
<div class='hide_text'>Delete Post: </div>
<form method="post" class="delete_post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" name="topic_id_value" id="topic_id_value" value="<?php echo $topic_id?>">
<button type="submit" name="delete" id="delete" class="btn btn-danger buttonElement">Delete</button>
</form>
</div>
</div>
</div>
<?php
}
?>
页面底部的JQUERY:
</body>
<?php include("../PHP/js.php"); ?>
<script type="text/javascript" src="../Members/updatedetails.js"></script>
<script type="text/javascript" src="../Members/bootbox.min.js"></script>
<script>
$('form').on('click','button.buttonElement',function (e) {
var form = $(this).closest('form'); // <-- add this
e.preventDefault();
bootbox.confirm({
message: "Are you sure you want to delete your post?",
title: "Delete Post",
buttons: {
cancel: {
label: "No",
className: "btn btn-default"
},
confirm: {
label: "Yes",
className: "btn btn-default"
}
},
callback: function(result) {
if(result == true) {
form.submit();
}
}
});
});
</script>
</html>
答案 0 :(得分:0)
最后弄清楚了为什么我的代码在我使用的php中无法正常工作
if(isset($ _ POST ['delete']))
但是使用javascript submit()
函数时,该名称attr被删除。因此,我使用了if(isset($_POST['topic_id_value']))
之类的隐藏输入的名称,它可以正常工作。
仍然要感谢那些给我建议的人