[英]Form not submitting after bootstrap confirmation
首先,我将道歉我的jquery / javascript的知识不是很好,所以我的错误对某些人来说可能非常明显,但我仍然在学习,所以要温柔!
我有一个表单,允许成员删除他们从数据库中发布的帖子。 表单工作正常,使用PHP删除数据库中的数据,如我所愿。 我想创建一个弹出窗口,让用户确认他们想做什么。 我不想使用bog标准浏览器特定的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>
最后弄清楚为什么我的代码不能在我的php中使用if(isset($ _ POST ['delete']))但是当使用javascript submit()
函数时,这个名称attr被删除了。 所以我使用隐藏输入的名称, if(isset($_POST['topic_id_value']))
并且它正常工作。
还要感谢那些给我建议的人
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.