javascript - 将数据插入弹出窗口(需要帮助)
问题描述
我做了一个弹出窗口,假设连接到 MySQL,它确实连接了,但我有 2 个按钮,它假设为它自己(个人)插入数据。目前,当我按下 1 个按钮时,一切正常,但当我按下第二个按钮时,它会显示第一个按钮的数据。我试图在互联网上查找,但找不到任何可以帮助我的东西。我的代码:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "megadrop";
// Create connection
$conne = new mysqli($servername, $username, $password, $dbname);
//check connection
if ($conne->connect_error) {
die("Connection failed: " . $conne->connect_error);
}
$sql = "SELECT * FROM uslugi";
$result = $conne->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($rowe = $result->fetch_assoc()) {
echo "<!-- The Modal -->";
echo "<div id='myModal' class='modal'>";
echo "<!-- Modal content -->";
echo "<div style='width:700px;height:300px;margin-top: 100px' class='modal-content'>";
echo "<div class='modal-header'>";
echo "<span class='close'>×</span>";
echo "<h2>".$rowe['nazwa']."<h2>";
echo "</div>";
echo "<div class='modal-body'>";
echo "<p>".$rowe['nazwa']."<p>";
echo "</div>";
echo "</div>";
echo "</div>";
}
}
$conne->close();
?>
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btns = document.getElementsByClassName("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
for (var i = 0; i < btns.length; i++) {
btns[i].onclick = function() {
modal.style.display = "block";
}
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
<?php
if(isset($_SESSION['zalogowany']))
echo "<p id='bczalogowany'>Jestes Zalogowany Do Panelu Gracza!</p>";
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "megadrop";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, nazwa, opis, cena, komenda, img, zakupiono FROM uslugi";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($usg = $result->fetch_assoc()) {
echo "<div class='usg'>";
echo "<p class='usg1'><img src='{$usg['img']}' width='75px; height: 75px;'</img></p> <br><br><br>";
echo "<p class='usg2'>".$usg['nazwa']."</p>";
echo "<p class='usg3'>".$usg['opis']."</p>";
echo "<p class='usg4'>".$usg['cena']."</p>";
echo "<button class='myBtn'>KUP ".$usg['nazwa']."</button>";
echo "</div>";
}
}
$conn->close();
?>
解决方案
推荐阅读
- java - 在 WhatApp 聊天中单击电子邮件链接时,如何让我的 Android 应用出现在应用选择器中
- sql - 如何从数据库中另一个表的 SELECT 查询中插入/更新表变量?
- reactjs - React Native Typescript:类型“string”不可分配给类型“solid”| “清楚” | “大纲”'.ts(2322)
- mvvm - 使用 TextWatcher 获取 editText 的旧值
- c# - C# ZipArchive - 如何嵌套内部 .zip 文件而不写入磁盘
- r - 覆盖随机森林和提升的决策边界
- javascript - Jest|Enzyme 如何在输入中的 defaultValue 上期望任何字符串
- python - 是否可以在开发计算机上不安装 ROS 的情况下用 Python 开发 ROS?
- python - 如何应用高斯朴素贝叶斯预测未来的交通量?
- python - 如何制作包含 744 小时的 31 天的数字范围