[英]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.