在Modal中输出MySQL数据
Output MySQL data in Modal
我一直在寻找解决我的问题的方法,并找到了一些使用 AJAX 的方法。但是,他们不为我工作。没有 AJAX 有没有办法做到这一点?
我已经为我整理了一个小门户来查看我客户的信息,我正在尝试在 MDBootstrap 模态中输出一些 MySQL 数据。除了模态进入的部分(只有第一个 DB 行进入)之外,一切都正常运行。这是我到目前为止所拥有的。
$con=mysqli_connect("localhost","db_user","dbpass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to database: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM clients");
echo "<table id='clientsInfo' class='table table-striped table-bordered table-sm cellspacing=0 width=100%'>
<thead>
<tr>
<th class='th-sm'>Client
</th>
<th class='th-sm'>Website URL
</th>
<th class='th-sm'>cPanel
</th>
<th class='th-sm'>Webmail
</th>
<th class='th-sm'>Website Admin
</th>
<th class='th-sm'>Cloudflare
</th>
<th class='th-sm'>Client Info
</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['client_name'] . "</td>";
echo "<td><a target='_blank' href='" . $row['website_url'] . "'><button type='button' class='btn blue-gradient btn-sm'>Click Here</button></a></td>";
echo "<td><a target='_blank' href='" . $row['cpanel'] . "'><button type='button' class='btn peach-gradient btn-sm'>Click Here</button></a></td>";
echo "<td><a target='_blank' href='" . $row['webmail'] . "'><button type='button' class='btn purple-gradient btn-sm'>Click Here</button></a></td>";
echo "<td><a target='_blank' href='" . $row['web_admin'] . "'><button type='button' class='btn blue-gradient btn-sm'>Click Here</button></a></td>";
echo "<td><a target='_blank' href='" . $row['cloudflare'] . "'><button type='button' class='btn peach-gradient btn-sm'>Click Here</button></a></td>";
echo "<td><button type='button' data-toggle='modal' data-target='#fullHeightModalRight' class='btn btn purple-gradient btn-sm'>Client Info</button></td>
<!-- Client Info Modal -->
<div class='modal fade right' id='fullHeightModalRight' tabindex='-1' role='dialog' aria-labelledby='clientInfoLabel'
aria-hidden='true'>
<!-- Add class .modal-full-height and then add class .modal-right (or other classes from list above) to set a position to the modal -->
<div class='modal-dialog modal-full-height modal-right' role='document'>
<div class='modal-content'>
<div class='modal-header'>
<h4 class='modal-title w-100' id='clientInfoLabel'>Hello World</h4>
<button type='button' class='close' data-dismiss='modal' aria-label='Close'>
<span aria-hidden='true'>×</span>
</button>
</div>
<div class='modal-body'>
" . $row['client_info'] . "
</div>
<div class='modal-footer justify-content-center'>
<button type='button' class='btn btn-secondary' data-dismiss='modal'>Close</button>
</div>
</div>
</div>
</div>
<!-- Client Info Modal -->";
echo "</tr>";
echo "</tbody>";
}
echo "<tfoot>";
echo "<tr>";
echo "<th>Client";
echo "</th>";
echo "<th>Website URL";
echo "</th>";
echo "<th>cPanel";
echo "</th>";
echo "<th>Webmail";
echo "</th>";
echo "<th>Website Admin";
echo "</th>";
echo "<th>Cloudflare";
echo "</th>";
echo "<th>Client Info";
echo "</th>";
echo "</tr>";
echo "</tfoot>";
echo "</table>";
mysqli_close($con);
?>```
从循环中删除 <tbody>
并将其放在循环之前。还要在循环后放 </tbody>
。
此外,从循环中删除此模式并将其粘贴到 HTML
部分末尾的 </body>
标记之前。
<!-- Client Info Modal -->
<div class='modal fade right' id='fullHeightModalRight' tabindex='-1' role='dialog' aria-labelledby='clientInfoLabel'
aria-hidden='true'>
<!-- Add class .modal-full-height and then add class .modal-right (or other classes from list above) to set a position to the modal -->
<div class='modal-dialog modal-full-height modal-right' role='document'>
<div class='modal-content'>
<div class='modal-header'>
<h4 class='modal-title w-100' id='clientInfoLabel'>Hello World</h4>
<button type='button' class='close' data-dismiss='modal' aria-label='Close'>
<span aria-hidden='true'>×</span>
</button>
</div>
<div class='modal-body' id="dynamic_contents"></div>
<div class='modal-footer justify-content-center'>
<button type='button' class='btn btn-secondary' data-dismiss='modal'>Close</button>
</div>
</div>
</div>
</div>
<!-- Client Info Modal -->
我刚刚添加了模态正文的 id dynamic_contents
然后添加一个 AJAX 函数,如下所示。
function open_modal(contents=null){
$('#dynamic_contents').html(contents);
$('#fullHeightModalRight').modal();
}
然后为 client_info
按钮添加 onClick
的触发器,如下所示。
echo '<td><button type="button" class="btn btn purple-gradient btn-sm" onclick="open_modal(\''.$row['client_info'].'\')">Client Info</button></td>';
我一直在寻找解决我的问题的方法,并找到了一些使用 AJAX 的方法。但是,他们不为我工作。没有 AJAX 有没有办法做到这一点? 我已经为我整理了一个小门户来查看我客户的信息,我正在尝试在 MDBootstrap 模态中输出一些 MySQL 数据。除了模态进入的部分(只有第一个 DB 行进入)之外,一切都正常运行。这是我到目前为止所拥有的。
$con=mysqli_connect("localhost","db_user","dbpass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to database: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM clients");
echo "<table id='clientsInfo' class='table table-striped table-bordered table-sm cellspacing=0 width=100%'>
<thead>
<tr>
<th class='th-sm'>Client
</th>
<th class='th-sm'>Website URL
</th>
<th class='th-sm'>cPanel
</th>
<th class='th-sm'>Webmail
</th>
<th class='th-sm'>Website Admin
</th>
<th class='th-sm'>Cloudflare
</th>
<th class='th-sm'>Client Info
</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['client_name'] . "</td>";
echo "<td><a target='_blank' href='" . $row['website_url'] . "'><button type='button' class='btn blue-gradient btn-sm'>Click Here</button></a></td>";
echo "<td><a target='_blank' href='" . $row['cpanel'] . "'><button type='button' class='btn peach-gradient btn-sm'>Click Here</button></a></td>";
echo "<td><a target='_blank' href='" . $row['webmail'] . "'><button type='button' class='btn purple-gradient btn-sm'>Click Here</button></a></td>";
echo "<td><a target='_blank' href='" . $row['web_admin'] . "'><button type='button' class='btn blue-gradient btn-sm'>Click Here</button></a></td>";
echo "<td><a target='_blank' href='" . $row['cloudflare'] . "'><button type='button' class='btn peach-gradient btn-sm'>Click Here</button></a></td>";
echo "<td><button type='button' data-toggle='modal' data-target='#fullHeightModalRight' class='btn btn purple-gradient btn-sm'>Client Info</button></td>
<!-- Client Info Modal -->
<div class='modal fade right' id='fullHeightModalRight' tabindex='-1' role='dialog' aria-labelledby='clientInfoLabel'
aria-hidden='true'>
<!-- Add class .modal-full-height and then add class .modal-right (or other classes from list above) to set a position to the modal -->
<div class='modal-dialog modal-full-height modal-right' role='document'>
<div class='modal-content'>
<div class='modal-header'>
<h4 class='modal-title w-100' id='clientInfoLabel'>Hello World</h4>
<button type='button' class='close' data-dismiss='modal' aria-label='Close'>
<span aria-hidden='true'>×</span>
</button>
</div>
<div class='modal-body'>
" . $row['client_info'] . "
</div>
<div class='modal-footer justify-content-center'>
<button type='button' class='btn btn-secondary' data-dismiss='modal'>Close</button>
</div>
</div>
</div>
</div>
<!-- Client Info Modal -->";
echo "</tr>";
echo "</tbody>";
}
echo "<tfoot>";
echo "<tr>";
echo "<th>Client";
echo "</th>";
echo "<th>Website URL";
echo "</th>";
echo "<th>cPanel";
echo "</th>";
echo "<th>Webmail";
echo "</th>";
echo "<th>Website Admin";
echo "</th>";
echo "<th>Cloudflare";
echo "</th>";
echo "<th>Client Info";
echo "</th>";
echo "</tr>";
echo "</tfoot>";
echo "</table>";
mysqli_close($con);
?>```
从循环中删除 <tbody>
并将其放在循环之前。还要在循环后放 </tbody>
。
此外,从循环中删除此模式并将其粘贴到 HTML
部分末尾的 </body>
标记之前。
<!-- Client Info Modal -->
<div class='modal fade right' id='fullHeightModalRight' tabindex='-1' role='dialog' aria-labelledby='clientInfoLabel'
aria-hidden='true'>
<!-- Add class .modal-full-height and then add class .modal-right (or other classes from list above) to set a position to the modal -->
<div class='modal-dialog modal-full-height modal-right' role='document'>
<div class='modal-content'>
<div class='modal-header'>
<h4 class='modal-title w-100' id='clientInfoLabel'>Hello World</h4>
<button type='button' class='close' data-dismiss='modal' aria-label='Close'>
<span aria-hidden='true'>×</span>
</button>
</div>
<div class='modal-body' id="dynamic_contents"></div>
<div class='modal-footer justify-content-center'>
<button type='button' class='btn btn-secondary' data-dismiss='modal'>Close</button>
</div>
</div>
</div>
</div>
<!-- Client Info Modal -->
我刚刚添加了模态正文的 id dynamic_contents
然后添加一个 AJAX 函数,如下所示。
function open_modal(contents=null){
$('#dynamic_contents').html(contents);
$('#fullHeightModalRight').modal();
}
然后为 client_info
按钮添加 onClick
的触发器,如下所示。
echo '<td><button type="button" class="btn btn purple-gradient btn-sm" onclick="open_modal(\''.$row['client_info'].'\')">Client Info</button></td>';