【问题标题】:Using PHP data with Javascript通过 Javascript 使用 PHP 数据
【发布时间】:2021-12-02 08:12:15
【问题描述】:

我显然有一个我无法解决的技术问题。首先,被称为“新手”对我来说非常好!这是我的问题的快速概述: 我有一个 MYSQL 数据库,其中包含许多家庭音频文件的数据。在根据内容搜索某些表格字段(标题、描述、标签...)时,我设法将数据返回到结果页面。

|标题 |描述 |标签 |播放音频|

|标题 1|描述 1 |标签 |按钮 |

|标题 2|描述 2 |标签 |按钮 |

我还修改了我在网上找到的一个简单的模态窗口代码,以创建一种弹出式音频播放器,它将显示有关文件的信息并允许播放它。我的目标是让用户单击播放音频下的特定按钮,它将打开模态框,并准备好播放适当的音频文件。但是,它不起作用。它将打开第一个,但不会打开以下任何一个。我确实知道 id 必须是唯一的,所以我尝试了这个

<div id="audioModal<?php echo $row['audio_id']; ?>" >
<button id="audioBtn<?php echo $row['audio_id']; ?>" >Play Audio</button>

我猜主要问题是当我尝试在 javascript 代码中使用它时,因为我无法在其中使用 php id。

<script type="text/javascript">
var modal = document.getElementById("audioModal");
// Get the button that opens the modal
var btn = document.getElementById("audioBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.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>

但没有运气。所有建议都非常受欢迎。提前致谢。

如果有帮助,这里是相关的 CSS。

<style> 
.modal{
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}


/* Modal Header */
.modal-header {
  padding: 2px 10px;
  background-color: #98a9b5;
  color: black;
}

h2{
    color: white;
    text-decoration: underline;
}

/* Modal Body */
.modal-body {
    padding: 2px 10px;
    /*margin: auto 20%; */
    background-color: #98a9b5;
    /*border: solid;*/
    }

/* Modal Footer */
.modal-footer {
  padding: 2px 10px;
  background-color: #3e6077;
  color: white;
}


/* Modal Content */
.modal-content {
  background-color: #3e6077;
  margin: 10% auto; /* 15% from the top and centered */
  padding: 10px;
  border: 1px solid #888;
  width: 30%; /* Could be more or less, depending on screen size */
}


/* The Close Button */
.close {
  color: white;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: black;
  text-decoration: none;
  cursor: pointer;
} 
</style>

【问题讨论】:

  • “我不能使用其中的 php id” - 为什么不能呢?是什么阻止您像在页面上的其他任何地方一样,在代码中您想要它的位置战略性地放置 &lt;?php echo $row['audio_id']; ?&gt;?当你尝试时会发生什么?
  • 谢谢@Robo Robok。这很可能是答案,但我不确定哪个选项最容易实施以及如何继续......
  • 嗨,大卫。当我尝试使用时出现问题 在 modal 和 btn 变量的 javascript 中。我试过这个: var modal = document.getElementById("audioModal");但不幸的是,它并没有解决我的问题。
  • @thecraw 你有一个模态元素还是多个?有很多方法可以处理您正在尝试做的事情,您可以考虑将音频 ID 放在按钮上的 data-audio-id 属性(例如)中。然后你可以点击event.currentTarget.dataset.audioId阅读它并使用这个值打开模式。

标签: javascript php html mysql


【解决方案1】:

getElementById('audioModal') 仅返回具有精确“audioModal”ID 的元素。当您创建了一堆唯一的 ID 时。

Javascript getElementById based on a partial string - 这个可能会对你有所帮助,但你需要获取按钮列表并为所有按钮分配相同的点击处理程序。老实说,这不是最好的方法。

【讨论】:

  • 感谢@Roman 的回答。看来我已经把自己逼到了一个角落里。任何关于最佳(和最简单)方法的建议将不胜感激。
猜你喜欢
  • 1970-01-01
  • 2023-04-10
  • 2010-10-12
  • 2017-03-21
  • 2016-05-14
  • 2017-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多