【发布时间】:2019-07-24 11:58:47
【问题描述】:
我有一个分 3 列显示 6 张引导卡的页面。每个 Bootstrap 卡都显示一个图像、名称、描述和页脚按钮。当用户点击 Bootstrap Card 页脚按钮时,一个 Bootstrap Modal 打开一个带有 Sql 行数据的 ID 并进入这个 Bootstrap Modal,有 2 个输入和一个 textarea 表单,如下所述:
1. <input type="text" name="name" id="name" class="form-control" required />
2. <input type="email" name="email" id="email" class="form-control" required />
3. <textarea class="form-control" type="text" name="message" id="message" rows="3"
required></textarea>
要根据 ID 打开 Modal,我在按钮卡代码中插入了一个 PHP 代码,如下所示:
<button type="button" class="btn btn-block btn-primary" data-toggle="modal"
data-target="#dataModal<?php echo $record['ID']; ?>">View Details</button>
<?php echo $record['ID']; ?> 与 SQL 行 ID 相关。
当用户填写表单并点击Modal提交按钮时,所有数据都发送到数据库,这没关系,但是Ajax成功后Bootstrap Modal不会隐藏。
我用来通过 php 代码 (insert.php) 在数据库中插入数据的 Ajax 脚本是:
<script type="text/javascript" language="javascript" >
$(document).on('submit', '#contactForm', function(event){
event.preventDefault();
$.ajax({
url:"insert.php",
method:'POST',
data:new FormData(this),
contentType:false,
processData:false,
success:function(data){
alert("Message sent!");
$("#contactForm")[0].reset();
$('#dataModal').hide();
$(".modal-backdrop").remove();
}
});
});
</script>
我认为问题与 Ajax 成功的这一行有关:$('#dataModal').hide(); 因为 Bootstrap Modal html id 属性是 "dataModal<?php echo $record['ID']; ?>" 而不仅仅是“dataModal”。
在这种情况下,如果我这样写$('#dataModal<?php echo $record['ID']; ?>').hide(); 什么都不会发生。
下面是完整的代码:
<div class="container">
<?php
include_once("includes/mysqli_connection.php");
$sql = "SELECT * FROM products ORDER BY RAND() LIMIT 6";
$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
while( $record = mysqli_fetch_assoc($resultset) ) {
?>
<div class="card-deck" style="width: 18rem; display:inline-block; margin:15px;">
<div class="card">
<?php echo '<img src="upload/'.$record["image"].'" class="img-thumbnail" width="286" height="180" />'; ?>
<div class="card-body" align="center"><h5 class="card-title"><?php echo $record['ProductName']; ?></h5></div>
<div class="card-footer">
<button type="button" class="btn btn-block btn-primary" data-toggle="modal" data-target="#dataModal<?php echo $record['ID']; ?>">View Details</button>
</div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="dataModal<?php echo $record['ID']; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" >
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" >Product: <?php echo $record['productName']; ?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="lead">Ask us about this product</div>
<form id="contactForm" method="post">
<div class="form-row">
<div class="col-md-4 mb-3">
<label>Name</label>
<input type="text" name="name" id="name" class="form-control" placeholder="Your name" required />
</div>
<div class="col-md-5 mb-3">
<label>E-mail</label>
<input type="email" name="email" id="email" class="form-control" placeholder="Your e-mail" required />
</div>
</div>
<div class="form-row">
<div class="col-md-12 mb-3">
<label>Message</label>
<textarea class="form-control" type="text" name="message" id="message" rows="3" placeholder="Your message" required></textarea>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">CLOSE</button>
<button type="submit" class="btn btn-success" id="submit" >SEND</button>
</form>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
<script type="text/javascript" language="javascript" >
$(document).on('submit', '#contactForm', function(event){
event.preventDefault();
$.ajax({
url:"insert.php",
method:'POST',
data:new FormData(this),
contentType:false,
processData:false,
success:function(data){
alert("Message sent!");
$("#contactForm")[0].reset();
$('#dataModal').hide();
$(".modal-backdrop").remove();
}
});
});
</script>
在这种情况下,它会是什么,我必须做什么?谢谢大家。
【问题讨论】:
-
我不确定这是否是拼写错误,但
$('#dataModal<?php echo $record['ID']; ?>'].hide();应该改为$('#dataModal<?php echo $record['ID']; ?>').hide();。).hide()之前应该有一个括号。您也可以使用$('#dataModal<?php echo $record['ID']; ?>').modal('hide')关闭模态框和背景。
标签: javascript php jquery ajax bootstrap-modal