[英]MySQL data not outputting onto bootstrap modal
我正在尝试将 output 数据放到我的模态中,但没有出现。 我已经设法 output 我创建的表上的一些数据。 但是,当尝试通过模态显示更多数据时,似乎什么也没有出现。
我一直在通过 PHP 进行潜在的修复,但没有运气。
下面的 php 是指在收到我的news.php
文件的信息后,将 output 数据放到模态上:
<?php
if(isset($_POST["employee_id"]))
{
$output = '';
$connect = mysqli_connect("localhost", "root", "test1234", "news_db");
$query = "SELECT * FROM latest_news WHERE news_id = '".$_POST["employee_id"]."'";
$result = mysqli_query($connect, $query);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td width="30%"><label>Name</label></td>
<td width="70%">'.$row["news_title"].'</td>
</tr>
<tr>
<td width="30%"><label>Address</label></td>
<td width="70%">'.$row["news_text"].'</td>
</tr>
';
}
$output .= "</table></div>";
echo $output;
}
?>
我的 news.php 文件中的这张表成功地从我的表中输出了所需的数据:
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="70%">Employee Name</th>
<th width="30%">View</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $row["news_title"]; ?></td>
<td><input type="button" name="view" value="view" id="<?php echo $row["news_id"]; ?>" class="btn btn-info btn-xs view_data" /></td>
</tr>
<?php
}
?>
</table>
</div>
<div id="dataModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Employee Details</h4>
</div>
<div class="modal-body" id="employee_detail"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
模态脚本:
<script>
$(document).ready(function(){
$('.view_data').click(function(){
var employee_id = $(this).attr("news_id");
$.ajax({
url:"select.php",
method:"post",
data:{employee_id:employee_id},
success:function(data){
$('#employee_detail').html(data);
$('#dataModal').modal("show");
}
});
});
});
</script>
您的代码看起来不错,但是您确定您使用的 SQL 查询是正确的吗?
$query = "SELECT * FROM latest_news WHERE news_id = '".$_POST["employee_id"]."'";
您正在使用employee_id
作为news_id
。 这可能是问题所在。 尝试获取$query
值并在您的数据库上运行它。
顺便说一句,考虑在 PHP 脚本上使用 PDO 和准备好的语句。 filter_input()
还可以帮助您的代码更安全
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.