如何通过单击超链接表格单元格使用引导程序4打开模式,并使用单击的ID从SQL数据库填充模型中的数据 [英] How do I open modal using bootstrap 4 by clicking a hyperlinked table cell and populate data in the model from SQL database using the clicked ID

查看:63
本文介绍了如何通过单击超链接表格单元格使用引导程序4打开模式,并使用单击的ID从SQL数据库填充模型中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,该页面在一个表中显示来自SQL的数据,并且在该表上一个单元格是超链接的,我希望一个模式(使用引导程序)使用ID(超链接值)打开和获取数据,并在其中填充数据SQL中的模式主体.我尝试了不同的方法,但是没有一种方法能很好地工作.模态打开了,但它并不突出,好像它在背景中打开一样.请帮助我.

I have a page which displays data from SQL in a table and on that table one cell is hyperlinked and I want a modal (using bootstrap) to open and fetch data using the ID (the hyperlink value) and populate with data on the body in modal from SQL. I have tried different approaches but none is working well. The modal opens up but its not prominent as if its opening in background. Please help me with this.

HEAD中使用的链接:

Used Links in HEAD:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script>

PHP代码

<?php
while ($ticketdata = mysqli_fetch_array($ticketresults)) 
{
// Print out the contents of the entry
echo '<tbody>';
echo '<th scope="row"><a href="#" data-toggle="modal" data-target="#ticketModal'.$ticketdata['ticketnumber'].'">'; 
echo $ticketdata['ticketnumber'];
echo '</a>';
echo '</th>';
echo '<td>' . $ticketdata['policynumber'] . '</td>';
echo '<td>' . $ticketdata['registration_number'] . '</td>';
echo '<td>' . $ticketdata['insuredname'] . '</td>';
echo '<td>' . $ticketdata['contactnumber'] . '</td>';
echo '<td>' . $ticketdata['casestatus'] . '</td>';
echo '<td>';
echo '<form method="post">';
echo '<input type="hidden" name="ticketnumber" value="'. $ticketdata['ticketnumber']. '">';
echo '<input type="submit" class="btn btn-danger btn-sm" name="deletedata" value="Delete">';
echo '</form>';
echo '</td>';
echo '</tbody>';
}
?>

如何将ID传递给模态,应该在哪里初始化传递ID(超链接)的模态,以便使用带有PHP脚本的ID来向模态填充数据?

How do I pass the ID to the modal and where should I initialize the modal passing the ID (hyperlink) to populate the modal with data using the ID with PHP script?

推荐答案

<table>
<?php
while($ticketdata = mysqli_fetch_array($ticketresults)){
<tr>  
   <td><a href="#" id="<?php echo $ticketdata['ticketnumber']; ?>" class="view_data" /><?php echo $ticketdata['ticketnumber']; ?></a></td>
 <td><?php echo $ticketdata['name']; ?></td> 
 <td><?php echo $ticketdata['policynumber']; ?></td> 
 <td><?php echo $ticketdata['registration_number']; ?></td> 
 <td><?php echo $ticketdata['insuredname']; ?></td> 
 <td><?php echo $ticketdata['contactnumber']; ?></td> 
 <td><?php echo $ticketdata['casestatus']; ?></td> 
 <td>DELETE</td> 
</tr>  
<?php } ?>  
</table>  
</body>  
</html>  


<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">&times;</button>  
                     <h4 class="modal-title">Policy Details</h4>  
                </div>  
                <div class="modal-body" id="detail">  
                </div>  
                <div class="modal-footer">  
                     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>  
                </div>  
           </div>  
      </div>  
 </div>  

Ajax代码:-

<script>  
 $(document).ready(function(){  
      $('.view_data').click(function(){  
           var ticket_id = $(this).attr("id");  
           $.ajax({  
                url:"select.php",  
                method:"post",  
                data:{ticket_id:ticket_id},  
                success:function(data){  
                     $('#detail').html(data);  
                     $('#dataModal').modal("show");  
                }  
           });  
      });  
 });  
 </script>

select.php:-

<?php  
 if(isset($_POST["ticket_id"]))  
 {  
      $output = '';  
      $connect = mysqli_connect("localhost", "root", "", "testing");  
      $query = "SELECT * FROM table_name WHERE id = '".$_POST["ticket_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>S. No</label></td>  
                     <td width="70%">'.$row["ticketnumber"].'</td>  
                </tr>  

                <tr>  
                     <td width="30%"><label>Ticket Name</label></td>  
                     <td width="70%">'.$row["name"].' Year</td>  
                </tr>  
                ';  
      }  
      $output .= "</table></div>";  
      echo $output;  
 }  
 ?>

这篇关于如何通过单击超链接表格单元格使用引导程序4打开模式,并使用单击的ID从SQL数据库填充模型中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆